|
| 1 | +# Playground and Cheatsheet for Learning Python |
| 2 | + |
| 3 | +> This is a collection of Python scripts that are split by [topics](#table-of-contents) and contain |
| 4 | +code examples with explanations, different use cases and links to further readings. |
| 5 | + |
| 6 | +It is a **playground** because you may change or add the code to see how it works |
| 7 | +and [test it out](#testing-the-code) using assertions. It also allows you |
| 8 | +to [lint the code](#linting-the-code) you've wrote and check if it fits to Python code style guide. |
| 9 | +Altogether it might make your learning process to be more interactive and it might help you to keep |
| 10 | +code quality pretty high from very beginning. |
| 11 | + |
| 12 | +It is a **cheatsheet** because you may get back to these code examples once you want to recap the |
| 13 | +syntax of [standard Python statements and constructions](#table-of-contents). Also because the |
| 14 | +code is full of assertions you'll be able to see expected functions/statements output right away |
| 15 | +without launching them. |
| 16 | + |
| 17 | +## How to Use This Repository |
| 18 | + |
| 19 | +Each Python script in this repository has the following structure: |
| 20 | + |
| 21 | +```python |
| 22 | +"""Lists <--- Name of the topic here |
| 23 | +
|
| 24 | +# @see: https://www.learnpython.org/en/Lists <-- Link to further readings goes here |
| 25 | +
|
| 26 | +Here might go more detailed explanation of the current topic (i.e. general info about Lists). |
| 27 | +""" |
| 28 | + |
| 29 | + |
| 30 | +def test_list_type(): |
| 31 | + """Explanation of sub-topic goes here. |
| 32 | + |
| 33 | + Each file contains test functions that illustrate sub-topics (i.e. lists type, lists methods). |
| 34 | + """ |
| 35 | + |
| 36 | + # Here is an example of how to build a list. <-- Comments here explain the action |
| 37 | + squares = [1, 4, 9, 16, 25] |
| 38 | + |
| 39 | + # Lists can be indexed and sliced. |
| 40 | + # Indexing returns the item. |
| 41 | + assert squares[0] == 1 # <-- Assertions here illustrate the result. |
| 42 | + # Slicing returns a new list. |
| 43 | + assert squares[-3:] == [9, 16, 25] # <-- Assertions here illustrate the result. |
| 44 | +``` |
| 45 | + |
| 46 | +So normally you might want to do the following: |
| 47 | + |
| 48 | +- [Find the topic](#table-of-contents) you're want to learn or recap. |
| 49 | +- Read comments and/or documentation that is linked in each script's docstring (as in example above). |
| 50 | +- Look at code examples and assertions to see usage examples and expected output. |
| 51 | +- Change code or add new assertions to see how things work. |
| 52 | +- [Run tests](#testing-the-code) and [lint the code](#linting-the-code) to see if it work and is |
| 53 | +written correctly. |
| 54 | + |
| 55 | +## Table of Contents |
| 56 | + |
| 57 | +1. **Getting Started** |
| 58 | + - [What is Python](src/getting_started/what_is_python.md) |
| 59 | + - [Python Syntax](src/getting_started/python_syntax.md) |
| 60 | + - [Variables](src/getting_started/test_variables.py) |
| 61 | +2. **Operators** |
| 62 | + - [Arithmetic Operators](src/operators/test_arithmetic.py) (`+`, `-`, `*`, `/`, `//`, `%`, `**`) |
| 63 | + - [Bitwise Operators](src/operators/test_bitwise.py) (`&`, `|`, `^`, `>>`, `<<`, `~`) |
| 64 | + - [Assignment Operators](src/operators/test_assigment.py) (`=`, `+=`, `-=`, `/=`, `//=` etc.) |
| 65 | + - [Comparison Operator](src/operators/test_comparison.py) (`==`, `!=`, `>`, `<`, `>=`, `<=`) |
| 66 | + - [Logical Operators](src/operators/test_logical.py) (`and`, `or`, `not`) |
| 67 | + - [Identity Operators](src/operators/test_identity.py) (`is`, `is not`) |
| 68 | + - [Membership Operators](src/operators/test_membership.py) (`in`, `not in`) |
| 69 | +3. **Data Types** |
| 70 | + - [Numbers](src/data_types/test_numbers.py) (including booleans) |
| 71 | + - [Strings](src/data_types/test_strings.py) and their methods |
| 72 | + - [Lists](src/data_types/test_lists.py) and their methods (including list comprehensions) |
| 73 | + - [Tuples](src/data_types/test_tuples.py) |
| 74 | + - [Sets](src/data_types/test_sets.py) and their methods |
| 75 | + - [Dictionaries](src/data_types/test_dictionaries.py) |
| 76 | + - [Type Casting](src/data_types/test_type_casting.py) |
| 77 | +4. **Control Flow** |
| 78 | + - [The `if` statement](src/control_flow/test_if.py) |
| 79 | + - [The `for` statement](src/control_flow/test_for.py) (and `range()` function) |
| 80 | + - [The `while` statement](src/control_flow/test_while.py) |
| 81 | + - [The `try` statements](src/control_flow/test_try.py) |
| 82 | + - [The `break` statement](src/control_flow/test_break.py) |
| 83 | + - [The `continue` statement](src/control_flow/test_break.py) |
| 84 | +5. **Functions** |
| 85 | + - [Function Definition](src/functions/test_function_definition.py) (`def` and `return` statements) |
| 86 | + - [Default Argument Values](src/functions/test_function_default_arguments.py) |
| 87 | + - [Keyword Arguments](src/functions/test_function_keyword_arguments.py) |
| 88 | + - [Arbitrary Argument Lists](src/functions/test_function_arbitrary_arguments.py) |
| 89 | + - [Unpacking Argument Lists](src/functions/test_function_unpacking_arguments.py) (`*` and `**` statements) |
| 90 | + - [Lambda Expressions](src/functions/test_lambda_expressions.py) (`lambda` statement) |
| 91 | + - [Documentation Strings](src/functions/test_function_documentation_string.py) |
| 92 | + - [Function Annotations](src/functions/test_function_annotations.py) |
| 93 | +6. **Classes** |
| 94 | + - [Class Definition](src/classes/test_class_definition.py) (`class` statement) |
| 95 | + - [Class Objects](src/classes/test_class_objects.py) |
| 96 | + - [Instance Objects](src/classes/test_instance_objects.py) |
| 97 | + - [Method Objects](src/classes/test_method_objects.py) |
| 98 | + - [Class and Instance Variables](src/classes/test_class_and_instance_variables.py) |
| 99 | + - [Inheritance](src/classes/test_inheritance.py) |
| 100 | + - [Multiple Inheritance](src/classes/test_multiple_inheritance.py) |
| 101 | +7. **Modules** |
| 102 | + - [Modules](src/modules/test_modules.py) (`import` statement) |
| 103 | + - [Packages](src/modules/test_packages.py) |
| 104 | +8. **Errors and Exceptions** |
| 105 | + - [Handling Exceptions](src/exceptions/test_handle_exceptions.py) (`try` statement) |
| 106 | + - [Raising Exceptions](src/exceptions/test_raise_exceptions.py) (`raise` statement) |
| 107 | +9. **Files** |
| 108 | + - [Reading and Writing](src/files/test_file_reading.py) (`with` statement) |
| 109 | + - [Methods of File Objects](src/files/test_file_methdos.py) |
| 110 | +10. **Additions** |
| 111 | + - [The `pass` statement](src/additions/test_pass.py) |
| 112 | + - [Generators](src/additions/test_generators.py) (`yield` statement) |
| 113 | +11. **Brief Tour of the Standard Libraries** |
| 114 | + - [Serialization](src/standard_libraries/test_json.py) (`json` library) |
| 115 | + - [File Wildcards](src/standard_libraries/test_glob.py) (`glob` library) |
| 116 | + - [String Pattern Matching](src/standard_libraries/test_re.py) (`re` library) |
| 117 | + - [Mathematics](src/standard_libraries/test_math.py) (`math`, `random`, `statistics` libraries) |
| 118 | + - [Dates and Times](src/standard_libraries/test_datetime.py) (`datetime` library) |
| 119 | + - [Data Compression](src/standard_libraries/test_zlib.py) (`zlib` library) |
| 120 | + |
| 121 | +## Prerequisites |
| 122 | + |
| 123 | +**Installing Python** |
| 124 | + |
| 125 | +Make sure that you have [Python3 installed](https://realpython.com/installing-python/) on your machine. |
| 126 | + |
| 127 | +You might want to use [venv](https://docs.python.org/3/library/venv.html) standard Python library |
| 128 | +to create virtual environments and have Python, pip and all dependent packages to be installed and |
| 129 | +served from the local project directory to avoid messing with system wide packages and their |
| 130 | +versions. |
| 131 | + |
| 132 | +Depending on your installation you might have access to Python3 interpreter either by |
| 133 | +running `python` or `python3`. The same goes for pip package manager - it may be accessible either |
| 134 | +by running `pip` or `pip3`. |
| 135 | + |
| 136 | +You may check your Python version by running: |
| 137 | + |
| 138 | +```bash |
| 139 | +python --version |
| 140 | +``` |
| 141 | + |
| 142 | +Note that in this repository whenever you see `python` it will be assumed that it is Python **3**. |
| 143 | + |
| 144 | +**Installing dependencies** |
| 145 | + |
| 146 | +Install all dependencies that are required for the project by running: |
| 147 | + |
| 148 | +```bash |
| 149 | +pip install -r requirements.txt |
| 150 | +``` |
| 151 | + |
| 152 | +## Testing the Code |
| 153 | + |
| 154 | +Tests are made using [pytest](https://docs.pytest.org/en/latest/) framework. |
| 155 | + |
| 156 | +You may add new tests for yourself by adding files and functions with `test_` prefix |
| 157 | +(i.e. `test_topic.py` with `def test_sub_topic()` function inside). |
| 158 | + |
| 159 | +To run all the tests please execute the following command from the project root folder: |
| 160 | + |
| 161 | +```bash |
| 162 | +pytest |
| 163 | +``` |
| 164 | + |
| 165 | +To run specific tests please execute: |
| 166 | + |
| 167 | +```bash |
| 168 | +pytest ./path/to/the/test_file.py |
| 169 | +``` |
| 170 | + |
| 171 | +## Linting the Code |
| 172 | + |
| 173 | +Linting is done using [pylint](http://pylint.pycqa.org/) library. |
| 174 | + |
| 175 | +To check if the code is written with respect |
| 176 | +to [PEP 8](https://www.python.org/dev/peps/pep-0008/) style guide please run: |
| 177 | + |
| 178 | +```bash |
| 179 | +pylint ./src/ |
| 180 | +``` |
| 181 | + |
| 182 | +In case if linter will detect error (i.e. `missing-docstring`) you may want to read more about |
| 183 | +specific error by running: |
| 184 | + |
| 185 | +```bash |
| 186 | +pylint --help-msg=missing-docstring |
| 187 | +``` |
0 commit comments