---
id: 1897
card_url: "https://mdrss.com/software-craft/languages-and-idioms/1897"
permalink_url: "https://mdrss.com/m/1897"
thread_url: "https://mdrss.com/s/software-craft"
markdown_url: "https://mdrss.com/software-craft/languages-and-idioms/1897/1897.md"
title: "Playground and Cheatsheet for Learning Python"
annotation: "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 using assertions."
state: published
thread: software-craft
domain: software-craft
category: languages-and-idioms
type: guide
tags: ["learning", "learning-by-doing", "learning-python", "programming-language", "python", "python3", "dev-tools", "cheatsheet"]
ontology_terms: []
relation_terms: []
license: "MIT"
version: 1
snapshot_at: "2026-08-04T12:19:48.524Z"
source_url: "https://github.com/trekhleb/learn-python"
source_kind: "mdrss-final-catalog"
platform_scam_risk: null
platform_evidence_score: 0
evidence_urls:
  - "https://github.com/trekhleb/learn-python"
---
# Playground and Cheatsheet for Learning Python

> 🇺🇦 UKRAINE [IS BEING ATTACKED](https://war.ukraine.ua/) BY RUSSIAN ARMY. CIVILIANS ARE GETTING KILLED. RESIDENTIAL AREAS ARE GETTING BOMBED.
> - Help Ukraine via:
>   - [Serhiy Prytula Charity Foundation](https://prytulafoundation.org/en/)
>   - [Come Back Alive Charity Foundation](https://savelife.in.ua/en/donate-en/)
>   - [National Bank of Ukraine](https://bank.gov.ua/en/news/all/natsionalniy-bank-vidkriv-spetsrahunok-dlya-zboru-koshtiv-na-potrebi-armiyi)
> - More info on [war.ukraine.ua](https://war.ukraine.ua/) and [MFA of Ukraine](https://twitter.com/MFA_Ukraine)

<hr/>

[![Build Status](https://travis-ci.org/trekhleb/learn-python.svg?branch=master)](https://travis-ci.org/trekhleb/learn-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.

> _Read this in:_ [_Português_](https://github.com/trekhleb/learn-python/blob/HEAD/README.pt-BR.md), [_Español_](https://github.com/trekhleb/learn-python/blob/HEAD/README.es-ES.md), [_Traditional Chinese_](https://github.com/trekhleb/learn-python/blob/HEAD/README.zh-TW.md), [_Українська_](https://github.com/trekhleb/learn-python/blob/HEAD/README.uk-UA.md).

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.

> _You might also be interested in 🤖 [Interactive Machine Learning Experiments](https://github.com/trekhleb/machine-learning-experiments)_

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

## 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/) and [flake8](http://flake8.pycqa.org/en/latest/) libraries.

### PyLint

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
```

[More about PyLint](http://pylint.pycqa.org/)

### Flake8

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
flake8 ./src
```

Or if you want to have more detailed output you may run:

```bash
flake8 ./src --statistics --show-source --count
```

[More about Flake8](http://flake8.pycqa.org/en/latest/)

## Author

- [@trekhleb](https://trekhleb.dev)
