Skip to content

Commit

Permalink
Add Python scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Aug 29, 2018
0 parents commit 77774a5
Show file tree
Hide file tree
Showing 68 changed files with 4,093 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
.pytest_cache
venv
**/*.pyc
env
__pycache__
.vscode
187 changes: 187 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Playground and Cheatsheet for Learning Python

> This is a collection of Python scripts that are split by [topics](#table-of-contents) and contain
code examples with explanations, different use cases and links to further readings.

It is a **playground** because you may change or add the code to see how it works
and [test it out](#testing-the-code) using assertions. It also allows you
to [lint the code](#linting-the-code) 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.

It is a **cheatsheet** because you may get back to these code examples once you want to recap the
syntax of [standard Python statements and constructions](#table-of-contents). Also because the
code is full of assertions you'll be able to see expected functions/statements output right away
without launching them.

## How to Use This Repository

Each Python script in this repository has the following structure:

```python
"""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.
```

So normally you might want to do the following:

- [Find the topic](#table-of-contents) you're want to learn or recap.
- Read comments and/or documentation that is linked in each script's docstring (as in example above).
- Look at code examples and assertions to see usage examples and expected output.
- Change code or add new assertions to see how things work.
- [Run tests](#testing-the-code) and [lint the code](#linting-the-code) to see if it work and is
written correctly.

## Table of Contents

1. **Getting Started**
- [What is Python](src/getting_started/what_is_python.md)
- [Python Syntax](src/getting_started/python_syntax.md)
- [Variables](src/getting_started/test_variables.py)
2. **Operators**
- [Arithmetic Operators](src/operators/test_arithmetic.py) (`+`, `-`, `*`, `/`, `//`, `%`, `**`)
- [Bitwise Operators](src/operators/test_bitwise.py) (`&`, `|`, `^`, `>>`, `<<`, `~`)
- [Assignment Operators](src/operators/test_assigment.py) (`=`, `+=`, `-=`, `/=`, `//=` etc.)
- [Comparison Operator](src/operators/test_comparison.py) (`==`, `!=`, `>`, `<`, `>=`, `<=`)
- [Logical Operators](src/operators/test_logical.py) (`and`, `or`, `not`)
- [Identity Operators](src/operators/test_identity.py) (`is`, `is not`)
- [Membership Operators](src/operators/test_membership.py) (`in`, `not in`)
3. **Data Types**
- [Numbers](src/data_types/test_numbers.py) (including booleans)
- [Strings](src/data_types/test_strings.py) and their methods
- [Lists](src/data_types/test_lists.py) and their methods (including list comprehensions)
- [Tuples](src/data_types/test_tuples.py)
- [Sets](src/data_types/test_sets.py) and their methods
- [Dictionaries](src/data_types/test_dictionaries.py)
- [Type Casting](src/data_types/test_type_casting.py)
4. **Control Flow**
- [The `if` statement](src/control_flow/test_if.py)
- [The `for` statement](src/control_flow/test_for.py) (and `range()` function)
- [The `while` statement](src/control_flow/test_while.py)
- [The `try` statements](src/control_flow/test_try.py)
- [The `break` statement](src/control_flow/test_break.py)
- [The `continue` statement](src/control_flow/test_break.py)
5. **Functions**
- [Function Definition](src/functions/test_function_definition.py) (`def` and `return` statements)
- [Default Argument Values](src/functions/test_function_default_arguments.py)
- [Keyword Arguments](src/functions/test_function_keyword_arguments.py)
- [Arbitrary Argument Lists](src/functions/test_function_arbitrary_arguments.py)
- [Unpacking Argument Lists](src/functions/test_function_unpacking_arguments.py) (`*` and `**` statements)
- [Lambda Expressions](src/functions/test_lambda_expressions.py) (`lambda` statement)
- [Documentation Strings](src/functions/test_function_documentation_string.py)
- [Function Annotations](src/functions/test_function_annotations.py)
6. **Classes**
- [Class Definition](src/classes/test_class_definition.py) (`class` statement)
- [Class Objects](src/classes/test_class_objects.py)
- [Instance Objects](src/classes/test_instance_objects.py)
- [Method Objects](src/classes/test_method_objects.py)
- [Class and Instance Variables](src/classes/test_class_and_instance_variables.py)
- [Inheritance](src/classes/test_inheritance.py)
- [Multiple Inheritance](src/classes/test_multiple_inheritance.py)
7. **Modules**
- [Modules](src/modules/test_modules.py) (`import` statement)
- [Packages](src/modules/test_packages.py)
8. **Errors and Exceptions**
- [Handling Exceptions](src/exceptions/test_handle_exceptions.py) (`try` statement)
- [Raising Exceptions](src/exceptions/test_raise_exceptions.py) (`raise` statement)
9. **Files**
- [Reading and Writing](src/files/test_file_reading.py) (`with` statement)
- [Methods of File Objects](src/files/test_file_methdos.py)
10. **Additions**
- [The `pass` statement](src/additions/test_pass.py)
- [Generators](src/additions/test_generators.py) (`yield` statement)
11. **Brief Tour of the Standard Libraries**
- [Serialization](src/standard_libraries/test_json.py) (`json` library)
- [File Wildcards](src/standard_libraries/test_glob.py) (`glob` library)
- [String Pattern Matching](src/standard_libraries/test_re.py) (`re` library)
- [Mathematics](src/standard_libraries/test_math.py) (`math`, `random`, `statistics` libraries)
- [Dates and Times](src/standard_libraries/test_datetime.py) (`datetime` library)
- [Data Compression](src/standard_libraries/test_zlib.py) (`zlib` library)

## Prerequisites

**Installing Python**

Make sure that you have [Python3 installed](https://realpython.com/installing-python/) on your machine.

You might want to use [venv](https://docs.python.org/3/library/venv.html) 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.

Depending on your installation you might have access to Python3 interpreter either by
running `python` or `python3`. The same goes for pip package manager - it may be accessible either
by running `pip` or `pip3`.

You may check your Python version by running:

```bash
python --version
```

Note that in this repository whenever you see `python` it will be assumed that it is Python **3**.

**Installing dependencies**

Install all dependencies that are required for the project by running:

```bash
pip install -r requirements.txt
```

## Testing the Code

Tests are made using [pytest](https://docs.pytest.org/en/latest/) framework.

You may add new tests for yourself by adding files and functions with `test_` prefix
(i.e. `test_topic.py` with `def test_sub_topic()` function inside).

To run all the tests please execute the following command from the project root folder:

```bash
pytest
```

To run specific tests please execute:

```bash
pytest ./path/to/the/test_file.py
```

## Linting the Code

Linting is done using [pylint](http://pylint.pycqa.org/) library.

To check if the code is written with respect
to [PEP 8](https://www.python.org/dev/peps/pep-0008/) style guide please run:

```bash
pylint ./src/
```

In case if linter will detect error (i.e. `missing-docstring`) you may want to read more about
specific error by running:

```bash
pylint --help-msg=missing-docstring
```
Loading

0 comments on commit 77774a5

Please sign in to comment.