Learn Python
Based on Python 3.12Python classes, encapsulation, duck typing and project layout, run in a browser
__init__ and self.Example: watering plants2EncapsulationHide a bank balance in __balance and open it for reading with @property. Only deposit and withdraw can change it.Example: bank account3InheritanceA bicycle and a truck inherit from a vehicle class. super() fills in the parent part first, then each child overrides only what differs.Example: bicycle and truck4PolymorphismCall the same save() on CSV, JSON and line-log objects, then let a class with no shared parent join in through duck typing.Example: exporting rows5AbstractionThe parent draws the report header and footer; each child fills only the body. The abc module stops a child that forgets it.Example: report template6Composition and class variablesA robot that keeps a battery object (composition), a class variable that tracks robots, and when to use @staticmethod and @classmethod.Example: robot and battery7Naming and accessHow name, _name, __name and __name__ differ, shown with a locker example and a table. Python protects data by agreement rather than locks.Example: code locker+Project structurePick a command-line tool, web API, data analysis project or library and get a src-layout tree, pyproject.toml and starter files as a zip.zip downloadSuggested order
Start with classes and objects and go in order. After encapsulation you can build one solid object; inheritance, polymorphism and abstraction show how classes work together.
Composition and class variables, then naming rules, answer questions left open earlier ("what instead of inheritance?", "what is the difference between one and two underscores?"). Finish by building a project folder in project structure.
Where the output comes from
Every example was run with Python 3.12 on our server and with the in-browser Python behind the Run button (Pyodide 0.27, Python 3.12.7), and the two outputs were compared by script. Step-through values were read from the running program with sys.settrace.
When you press Run, the code runs inside your browser. On your own computer, run it like this.
python3 main.py # on Windowspy main.pyPython version
Text and examples follow Python 3.12. Most of it runs unchanged on 3.10 and newer; where an error message differs between versions, the page says so.
Where Python came from
Guido van Rossum started Python in late 1989 while working at CWI, the Dutch national research institute for mathematics and computer science. Version 0.9.0 went public in February 1991, 2.0 followed in 2000 and 3.0 in December 2008. Because 3.0 broke a lot of old code, the move took more than ten years, and support for 2.7 ended in January 2020. The Python Software Foundation, a non-profit set up in 2001, holds the rights, and since 2019 a five-person steering council elected by the core developers sets the direction of the language.
Data analysis (pandas), machine learning (PyTorch, scikit-learn), web back ends (Django, FastAPI), office automation and scientific computing all lean on Python. The short syntax is easy to pick up and the package ecosystem is huge, so you get results fast. It runs slower than compiled languages, though, and it is rarely the choice for phone apps or in-browser interfaces.
Examples on this site run right in your browser. Start with classes and objects, change the code a little and run it again, and use Compare languages to see where Python and Java part ways.
Tools that pair well Compare languages Python project structure JSON to CSV
| Made by | CWI (Netherlands), Guido van Rossum |
|---|---|
| First release | 0.9.0 in February 1991 · 3.0 in December 2008 |
| Run today by | Python Software Foundation and the steering council |
| Used mostly for | Data analysis, machine learning, web back ends, automation |
Questions people ask
I am new to Python. Where should I start?
Start with classes and objects and go in order. The pages assume you know basics such as variables, if, for and functions.
Where does my code run when I press Run?
Inside your browser. The first press downloads an in-browser Python once, and later runs start at once.
Which Python version do the examples need?
They were checked on Python 3.12. Most run unchanged on 3.10 and newer, and error messages that differ by version are noted on the page.