Skip to content

Guide · Open source

How to use learn-python: run and edit Python examples

Updated · written with AI from the project's own documentation and checked against our catalogue data

learn-python is an open-source (MIT licensed) collection of Python scripts split by topic, such as variables, lists, classes and files. Each script pairs code examples with comments, docstrings and assertions that show the expected output. This guide explains how to get it running locally, how to read the scripts, and how to use them as a playground and cheatsheet.

learn-pythontrekhleb/learn-python · ★ 18.5k · MIT

Quick start

  1. 1Check Python 3. Run python --version to confirm that Python 3 is installed. Use python3 if your system needs it.
  2. 2Clone the repository. Run git clone https://github.com/trekhleb/learn-python.git to download the project.
  3. 3Install dependencies. Move into the project folder and run pip install -r requirements.txt.
  4. 4Pick a topic. Open a script from the table of contents, such as the lists or classes file.
  5. 5Read and run the examples. Read the comments and assertions, then edit the code and run it to see how the behaviour changes.
  6. 6Run the tests. Use pytest to run the test functions and confirm the assertions pass after your changes.

What the repository contains

The project is described in its README as both a playground and a cheatsheet. As a playground, you can change or add code to see how it works, test it with assertions, and lint it against the Python style guide. As a cheatsheet, you can return to the examples to recap the syntax of standard Python statements and constructions.

  • Getting Started: what Python is and its syntax
  • Operators: arithmetic, bitwise, comparison, logical, identity and membership
  • Data Types: numbers, strings, lists, tuples, sets, dictionaries and type casting
  • Control Flow: if, for, while, try, break and continue
  • Functions, Classes, Modules, Errors and Exceptions, Files
  • Additions, a brief tour of standard libraries, and user input

Install Python 3 and pip

The README asks you to have Python 3 installed. Depending on your setup, the interpreter may be called python or python3, and pip may be called pip or pip3. The README assumes that whenever it says python, it means Python 3.

To check your version, run the command below. The README also suggests using the standard venv library to keep project packages separate from system-wide packages.

python --version

Clone the repository and install dependencies

Clone the repository from GitHub, move into its folder, and install the packages listed in requirements.txt. The README gives these two steps as the setup for the project.

git clone https://github.com/trekhleb/learn-python.git pip install -r requirements.txt

Read a topic script

Each Python script starts with a docstring that names the topic and links to further reading. Inside, test functions illustrate sub-topics. Comments explain each action, and assertions show the result. For example, a lists script builds a list, then uses assertions to show that indexing returns an item while slicing returns a new list.

A good way to study is to pick a topic from the table of contents, read the comments and linked docs, look at the assertions, and then change the code or add new assertions to see what happens.

  • Find the topic you want in the table of contents
  • Read the comments and the docstring links
  • Study the examples and assertions
  • Change code or add assertions and observe the results

Run the tests and lint the code

Tests use the pytest framework. You can add your own tests by creating files and functions with the test_ prefix, for example test_topic.py with a def test_sub_topic() function. The README also describes linting your code to check that it follows the Python style guide. Running the tests requires the local setup from the previous steps.

What people use it for

  • Recapping the syntax of a statement, such as the for loop or with statement, before writing code
  • Practising list slicing, dictionaries or class inheritance by changing the examples and rerunning them
  • Checking code style with a linter while working through the examples
  • Adding your own test_ functions to explore how a construct behaves

Frequently asked questions

Is learn-python free to use?

Yes. The repository is licensed under MIT, which the listing states. You can read, change and reuse the code under that licence.

What do I need to install to use learn-python?

You need Python 3 and pip. Then clone the repository and run pip install -r requirements.txt. Running the tests also requires pytest, which the README covers under its testing section.

Is learn-python a complete Python course?

No. The README describes topic-by-topic examples rather than a structured curriculum with projects. It works best alongside other resources.

How is learn-python different from learnpython.org?

learn-python is a local set of scripts you run and edit with assertions, so you work in your own environment. learnpython.org is an online tutorial, and the scripts link to it for further reading.

Does learn-python cover advanced or domain-specific Python?

Not really. The table of contents covers core language features and basic standard library modules, so advanced or specialised topics are outside its scope.

Why do my tests fail or not run?

Check that Python 3 and pip are installed, that you ran pip install -r requirements.txt from the project folder, and that pytest is available. The README notes that you might need python3 or pip3 depending on your installation.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

learn-python suits beginners who want to read and run small Python examples, and developers who want a quick syntax reference and a linter-friendly set of scripts. It is not a full course with projects, so people who want a structured curriculum or advanced topics should choose something like Automate the Boring Stuff with Python or the official Python tutorial on docs.python.org.

Install options & downloads