-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.html
497 lines (490 loc) · 16.8 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>README</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
</head>
<body>
<h1 id="playground-and-cheatsheet-for-learning-python">
Playground and Cheatsheet for Learning Python
</h1>
<p>
<a href="https://travis-ci.org/trekhleb/learn-python"
><img
src="https://travis-ci.org/trekhleb/learn-python.svg?branch=master"
alt="Build Status"
/></a>
</p>
<blockquote>
<p>
This is a collection of Python scripts that are split by
<a href="#table-of-contents">topics</a> and contain code examples with
explanations, different use cases and links to further readings.
</p>
</blockquote>
<p>
<em>Read this in</em> <a href="README.pt-BR.md"><em>Português</em></a
>.
</p>
<p>
It is a <strong>playground</strong> because you may change or add the code
to see how it works and <a href="#testing-the-code">test it out</a> using
assertions. It also allows you to
<a href="#linting-the-code">lint the code</a> you’ve wrote and check if it
fits to Python code style guide. Altogether it might make your learning
process to be more interactive and it might help you to keep code quality
pretty high from very beginning.
</p>
<p>
It is a <strong>cheatsheet</strong> because you may get back to these code
examples once you want to recap the syntax of
<a href="#table-of-contents"
>standard Python statements and constructions</a
>. Also because the code is full of assertions you’ll be able to see
expected functions/statements output right away without launching them.
</p>
<blockquote>
<p>
<em
>You might also be interested in 🤖
<a href="https://github.com/trekhleb/machine-learning-experiments"
>Interactive Machine Learning Experiments</a
></em
>
</p>
</blockquote>
<h2 id="how-to-use-this-repository">How to Use This Repository</h2>
<p>Each Python script in this repository has the following structure:</p>
<pre><code>"""Lists <--- Name of the topic here
# @see: https://www.learnpython.org/en/Lists <-- Link to further readings goes here
Here might go more detailed explanation of the current topic (i.e. general info about Lists).
"""
def test_list_type():
"""Explanation of sub-topic goes here.
Each file contains test functions that illustrate sub-topics (i.e. lists type, lists methods).
"""
# Here is an example of how to build a list. <-- Comments here explain the action
squares = [1, 4, 9, 16, 25]
# Lists can be indexed and sliced.
# Indexing returns the item.
assert squares[0] == 1 # <-- Assertions here illustrate the result.
# Slicing returns a new list.
assert squares[-3:] == [9, 16, 25] # <-- Assertions here illustrate the result.</code></pre>
<p>So normally you might want to do the following:</p>
<ul>
<li>
<a href="#table-of-contents">Find the topic</a> you want to learn or
recap.
</li>
<li>
Read comments and/or documentation that is linked in each script’s
docstring (as in example above).
</li>
<li>
Look at code examples and assertions to see usage examples and expected
output.
</li>
<li>Change code or add new assertions to see how things work.</li>
<li>
<a href="#testing-the-code">Run tests</a> and
<a href="#linting-the-code">lint the code</a> to see if it work and is
written correctly.
</li>
</ul>
<h2 id="table-of-contents">Table of Contents</h2>
<ol type="1">
<li>
<strong>Getting Started</strong>
<ul>
<li>
<a href="src/getting_started/what_is_python.md">What is Python</a>
</li>
<li>
<a href="src/getting_started/python_syntax.md">Python Syntax</a>
</li>
<li><a href="src/getting_started/test_variables.py">Variables</a></li>
</ul>
</li>
<li>
<strong>Operators</strong>
<ul>
<li>
<a href="src/operators/test_arithmetic.py">Arithmetic Operators</a>
(<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>,
<code>//</code>, <code>%</code>, <code>**</code>)
</li>
<li>
<a href="src/operators/test_bitwise.py">Bitwise Operators</a>
(<code>&</code>, <code>|</code>, <code>^</code>,
<code>>></code>, <code><<</code>, <code>~</code>)
</li>
<li>
<a href="src/operators/test_assigment.py">Assignment Operators</a>
(<code>=</code>, <code>+=</code>, <code>-=</code>, <code>/=</code>,
<code>//=</code> etc.)
</li>
<li>
<a href="src/operators/test_comparison.py">Comparison Operator</a>
(<code>==</code>, <code>!=</code>, <code>></code>,
<code><</code>, <code>>=</code>, <code><=</code>)
</li>
<li>
<a href="src/operators/test_logical.py">Logical Operators</a>
(<code>and</code>, <code>or</code>, <code>not</code>)
</li>
<li>
<a href="src/operators/test_identity.py">Identity Operators</a>
(<code>is</code>, <code>is not</code>)
</li>
<li>
<a href="src/operators/test_membership.py">Membership Operators</a>
(<code>in</code>, <code>not in</code>)
</li>
</ul>
</li>
<li>
<strong>Data Types</strong>
<ul>
<li>
<a href="src/data_types/test_numbers.py">Numbers</a> (including
booleans)
</li>
<li>
<a href="src/data_types/test_strings.py">Strings</a> and their
methods
</li>
<li>
<a href="src/data_types/test_lists.py">Lists</a> and their methods
(including list comprehensions)
</li>
<li><a href="src/data_types/test_tuples.py">Tuples</a></li>
<li>
<a href="src/data_types/test_sets.py">Sets</a> and their methods
</li>
<li>
<a href="src/data_types/test_dictionaries.py">Dictionaries</a>
</li>
<li>
<a href="src/data_types/test_type_casting.py">Type Casting</a>
</li>
</ul>
</li>
<li>
<strong>Control Flow</strong>
<ul>
<li>
<a href="src/control_flow/test_if.py"
>The <code>if</code> statement</a
>
</li>
<li>
<a href="src/control_flow/test_for.py"
>The <code>for</code> statement</a
>
(and <code>range()</code> function)
</li>
<li>
<a href="src/control_flow/test_while.py"
>The <code>while</code> statement</a
>
</li>
<li>
<a href="src/control_flow/test_try.py"
>The <code>try</code> statements</a
>
</li>
<li>
<a href="src/control_flow/test_break.py"
>The <code>break</code> statement</a
>
</li>
<li>
<a href="src/control_flow/test_continue.py"
>The <code>continue</code> statement</a
>
</li>
</ul>
</li>
<li>
<strong>Functions</strong>
<ul>
<li>
<a href="src/functions/test_function_definition.py"
>Function Definition</a
>
(<code>def</code> and <code>return</code> statements)
</li>
<li>
<a href="src/functions/test_function_scopes.py"
>Scopes of Variables Inside Functions</a
>
(<code>global</code> and <code>nonlocal</code> statements)
</li>
<li>
<a href="src/functions/test_function_default_arguments.py"
>Default Argument Values</a
>
</li>
<li>
<a href="src/functions/test_function_keyword_arguments.py"
>Keyword Arguments</a
>
</li>
<li>
<a href="src/functions/test_function_arbitrary_arguments.py"
>Arbitrary Argument Lists</a
>
</li>
<li>
<a href="src/functions/test_function_unpacking_arguments.py"
>Unpacking Argument Lists</a
>
(<code>*</code> and <code>**</code> statements)
</li>
<li>
<a href="src/functions/test_lambda_expressions.py"
>Lambda Expressions</a
>
(<code>lambda</code> statement)
</li>
<li>
<a href="src/functions/test_function_documentation_string.py"
>Documentation Strings</a
>
</li>
<li>
<a href="src/functions/test_function_annotations.py"
>Function Annotations</a
>
</li>
<li>
<a href="src/functions/test_function_decorators.py"
>Function Decorators</a
>
</li>
</ul>
</li>
<li>
<strong>Classes</strong>
<ul>
<li>
<a href="src/classes/test_class_definition.py">Class Definition</a>
(<code>class</code> statement)
</li>
<li><a href="src/classes/test_class_objects.py">Class Objects</a></li>
<li>
<a href="src/classes/test_instance_objects.py">Instance Objects</a>
</li>
<li>
<a href="src/classes/test_method_objects.py">Method Objects</a>
</li>
<li>
<a href="src/classes/test_class_and_instance_variables.py"
>Class and Instance Variables</a
>
</li>
<li><a href="src/classes/test_inheritance.py">Inheritance</a></li>
<li>
<a href="src/classes/test_multiple_inheritance.py"
>Multiple Inheritance</a
>
</li>
</ul>
</li>
<li>
<strong>Modules</strong>
<ul>
<li>
<a href="src/modules/test_modules.py">Modules</a> (<code
>import</code
>
statement)
</li>
<li><a href="src/modules/test_packages.py">Packages</a></li>
</ul>
</li>
<li>
<strong>Errors and Exceptions</strong>
<ul>
<li>
<a href="src/exceptions/test_handle_exceptions.py"
>Handling Exceptions</a
>
(<code>try</code> statement)
</li>
<li>
<a href="src/exceptions/test_raise_exceptions.py"
>Raising Exceptions</a
>
(<code>raise</code> statement)
</li>
</ul>
</li>
<li>
<strong>Files</strong>
<ul>
<li>
<a href="src/files/test_file_reading.py">Reading and Writing</a>
(<code>with</code> statement)
</li>
<li>
<a href="src/files/test_file_methods.py">Methods of File Objects</a>
</li>
</ul>
</li>
<li>
<strong>Additions</strong>
<ul>
<li>
<a href="src/additions/test_pass.py"
>The <code>pass</code> statement</a
>
</li>
<li>
<a href="src/additions/test_generators.py">Generators</a> (<code
>yield</code
>
statement)
</li>
</ul>
</li>
<li>
<strong>Brief Tour of the Standard Libraries</strong>
<ul>
<li>
<a href="src/standard_libraries/test_json.py">Serialization</a>
(<code>json</code> library)
</li>
<li>
<a href="src/standard_libraries/test_glob.py">File Wildcards</a>
(<code>glob</code> library)
</li>
<li>
<a href="src/standard_libraries/test_re.py"
>String Pattern Matching</a
>
(<code>re</code> library)
</li>
<li>
<a href="src/standard_libraries/test_math.py">Mathematics</a>
(<code>math</code>, <code>random</code>,
<code>statistics</code> libraries)
</li>
<li>
<a href="src/standard_libraries/test_datetime.py"
>Dates and Times</a
>
(<code>datetime</code> library)
</li>
<li>
<a href="src/standard_libraries/test_zlib.py">Data Compression</a>
(<code>zlib</code> library)
</li>
</ul>
</li>
</ol>
<h2 id="prerequisites">Prerequisites</h2>
<p><strong>Installing Python</strong></p>
<p>
Make sure that you have
<a href="https://realpython.com/installing-python/">Python3 installed</a>
on your machine.
</p>
<p>
You might want to use
<a href="https://docs.python.org/3/library/venv.html">venv</a> standard
Python library to create virtual environments and have Python, pip and all
dependent packages to be installed and served from the local project
directory to avoid messing with system wide packages and their versions.
</p>
<p>
Depending on your installation you might have access to Python3
interpreter either by running <code>python</code> or <code>python3</code>.
The same goes for pip package manager - it may be accessible either by
running <code>pip</code> or <code>pip3</code>.
</p>
<p>You may check your Python version by running:</p>
<pre><code>python --version</code></pre>
<p>
Note that in this repository whenever you see <code>python</code> it will
be assumed that it is Python <strong>3</strong>.
</p>
<p><strong>Installing dependencies</strong></p>
<p>
Install all dependencies that are required for the project by running:
</p>
<pre><code>pip install -r requirements.txt</code></pre>
<h2 id="testing-the-code">Testing the Code</h2>
<p>
Tests are made using
<a href="https://docs.pytest.org/en/latest/">pytest</a> framework.
</p>
<p>
You may add new tests for yourself by adding files and functions with
<code>test_</code> prefix (i.e. <code>test_topic.py</code> with
<code>def test_sub_topic()</code> function inside).
</p>
<p>
To run all the tests please execute the following command from the project
root folder:
</p>
<pre><code>pytest</code></pre>
<p>To run specific tests please execute:</p>
<pre><code>pytest ./path/to/the/test_file.py</code></pre>
<h2 id="linting-the-code">Linting the Code</h2>
<p>
Linting is done using <a href="http://pylint.pycqa.org/">pylint</a> and
<a href="http://flake8.pycqa.org/en/latest/">flake8</a> libraries.
</p>
<h3 id="pylint">PyLint</h3>
<p>
To check if the code is written with respect to
<a href="https://www.python.org/dev/peps/pep-0008/">PEP 8</a> style guide
please run:
</p>
<pre><code>pylint ./src/</code></pre>
<p>
In case if linter will detect error (i.e. <code>missing-docstring</code>)
you may want to read more about specific error by running:
</p>
<pre><code>pylint --help-msg=missing-docstring</code></pre>
<p><a href="http://pylint.pycqa.org/">More about PyLint</a></p>
<h3 id="flake8">Flake8</h3>
<p>
To check if the code is written with respect to
<a href="https://www.python.org/dev/peps/pep-0008/">PEP 8</a> style guide
please run:
</p>
<pre><code>flake8 ./src</code></pre>
<p>Or if you want to have more detailed output you may run:</p>
<pre><code>flake8 ./src --statistics --show-source --count</code></pre>
<p><a href="http://flake8.pycqa.org/en/latest/">More about Flake8</a></p>
<h2 id="supporting-the-project">Supporting the project</h2>
<p>
You may support this project via ❤️️
<a href="https://github.com/sponsors/trekhleb">GitHub</a> or ❤️️
<a href="https://www.patreon.com/trekhleb">Patreon</a>.
</p>
</body>
</html>