trekhleb/learn-python is a server-side project on GitHub with 18.3k stars, written primarily in Python. 📚 Playground and cheatsheet for learning Python. Collection of Python scripts that are split by topics and contain code examples with explanations.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
🇺🇦 UKRAINE IS BEING ATTACKED BY RUSSIAN ARMY. CIVILIANS ARE GETTING KILLED. RESIDENTIAL AREAS ARE GETTING BOMBED.
Help Ukraine via:
Serhiy Prytula Charity Foundation
Come Back Alive Charity Foundation
National Bank of Ukraine
More info on war.ukraine.ua and MFA of Ukraine
This is a collection of Python scripts that are split by topics and contain
code examples with explanations, different use cases and links to further readings.
Read this in:Português, Español, Traditional Chinese, Українська.
It is a playground because you may change or add the code to see how it works
and test it out using assertions. It also allows you
to lint 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. 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
How to Use This Repository
Each Python script in this repository has the following structure:
"""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 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 and lint the code to see if it work and is
written correctly.
Table of Contents
Getting Started
What is Python
Python Syntax
Variables
Operators
Arithmetic Operators (+, -, *, /, //, %, **)
Bitwise Operators (&, |, ^, >>, <<, ~)
Assignment Operators (=, +=, -=, /=, //= etc.)
Comparison Operator (==, !=, >, <, >=, <=)
Logical Operators (and, or, not)
Identity Operators (is, is not)
Membership Operators (in, not in)
Data Types
Numbers (including booleans)
Strings and their methods
Lists and their methods (including list comprehensions)
Tuples
Sets and their methods
Dictionaries
Type Casting
Control Flow
The if statement
The for statement (and range() function)
The while statement
The try statements
The break statement
The continue statement
Functions
Function Definition (def and return statements)
Scopes of Variables Inside Functions (global and nonlocal statements)
Default Argument Values
Keyword Arguments
Arbitrary Argument Lists
Unpacking Argument Lists (* and ** statements)
Lambda Expressions (lambda statement)
Documentation Strings
Function Annotations
Function Decorators
Classes
Class Definition (class statement)
Class Objects
Instance Objects
Method Objects
Class and Instance Variables
Inheritance
Multiple Inheritance
Modules
Modules (import statement)
Packages
Errors and Exceptions
Handling Exceptions (try statement)
Raising Exceptions (raise statement)
Files
Reading and Writing (with statement)
Methods of File Objects
Additions
The pass statement
Generators (yield statement)
Brief Tour of the Standard Libraries
Serialization (json library)
File Wildcards (glob library)
String Pattern Matching (re library)
Mathematics (math, random, statistics libraries)
Dates and Times (datetime library)
Data Compression (zlib library)
User input
Terminal input (input statement)
Prerequisites
Installing Python
Make sure that you have Python3 installed on your machine.
You might want to use venv 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:
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:
pip install -r requirements.txt
Testing the Code
Tests are made using pytest 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:
pytest
To run specific tests please execute:
pytest ./path/to/the/test_file.py
Linting the Code
Linting is done using pylint and flake8 libraries.
PyLint
To check if the code is written with respect
to PEP 8 style guide please run:
pylint ./src/
In case if linter will detect error (i.e. missing-docstring) you may want to read more about
specific error by running:
pylint --help-msg=missing-docstring
More about PyLint
Flake8
To check if the code is written with respect
to PEP 8 style guide please run:
flake8 ./src
Or if you want to have more detailed output you may run:
Does trekhleb/learn-python have a project website?
No homepage URL was recorded for trekhleb/learn-python in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How active is development on trekhleb/learn-python?
The most recent commit recorded on trekhleb/learn-python was 5 months ago, based on the GitHub push timestamp. The repository has 3.0k forks — one of the better signals of community interest.
Is trekhleb/learn-python open source?
Yes — trekhleb/learn-python ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/trekhleb/learn-python.
What license does trekhleb/learn-python use?
trekhleb/learn-python is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
What topics is trekhleb/learn-python associated with?
GitHub's repository topics for trekhleb/learn-python: "learning", "learning-by-doing", "learning-python", "programming-language", "python", "python3". TopGit's editorial category is Backend.
Where do I read more about trekhleb/learn-python?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/trekhleb/learn-python is the definitive source.
Read full README in the tab above.
Is learn-python worth your time?
ChatGPT, Claude and Perplexity can all read this page. Ask one of them what it makes of learn-python.