Beginner Python
Syntax, types, control flow, functions, the built-in collections, errors, files and modules. No experience needed.
-
Lesson 14 min read
Python Hello World: your first program, step by step
Install Python 3.14, run it two ways, and read an eleven-line program one line at a time.
-
Short note2 min read
Python IndentationError explained: whitespace is syntax
Why four spaces decide what your program does, shown with one line that moves.
-
Short note2 min read
Python REPL vs .py file: two ways to run Python
When to type into the interactive shell, when to save a file, and what Python 3.14 added to the shell.
-
Lesson 25 min read
Python variables and data types: int, float, str, bool, None
The five basic types, how to ask Python what type a value is, and how to turn one type into another.
-
Short note2 min read
Python int overflow: why 2 ** 1000 just works
Most languages keep a whole number in 64 bits and overflow past it. A Python int has no limit, and the reason also explains why floats still do.
-
Short note2 min read
Python type() function: why 3, 3.0 and "3" are different
Three values that look alike are an int, a float and a str. type() shows the difference, and mixing them shows why it matters.
-
Lesson 35 min read
Python if else and loops: if, elif, for, while, break, continue
How a program decides with if, elif and else, repeats with for and while, and stops or skips a round with break and continue.
-
Short note2 min read
Python chained comparison: 0 < x < 10 just works
In most languages that line is a bug. In Python it is one test that means exactly what it says, and this note shows how Python reads it.
-
Short note2 min read
Python for else: the else that means no break
A Python for loop can have an else block, and it does not mean what you think. It runs only when the loop finished on its own, with no break.
-
Lesson 45 min read
Python strings: indexing, slicing, methods and f-strings
Pick characters out of a Python string, slice pieces off it, clean it up with methods like strip and split, and build new text with f-strings.
-
Short note2 min read
Python f-string debugging: print the name and value with =
Put an equals sign after a name inside an f-string and Python prints the name, the equals sign and the value, so debug prints never need the name typed twice.
-
Short note2 min read
Reverse a string in Python with slicing: [::-1]
Slice a Python string with a step of minus one and you get it backwards, with no loop and no function. The same trick checks for a palindrome.
-
Lesson 55 min read
Python lists: index, slice, append, pop and sort
Build a Python list, index and slice it, grow and shrink it with append, insert and pop, sort it, and learn what each move costs.
-
Short note2 min read
Python list insert at the front is slow: use deque appendleft
nums.insert(0, x) shifts every item in a Python list one place right, so it gets slower as the list grows. A deque adds to the front without moving anything.
-
Short note2 min read
Python negative indexing: get the last item with nums[-1]
A negative index counts from the end of a Python list, so nums[-1] is always the last item, with no len() needed.
-
Lesson 65 min read
Python tuples and sets: unpacking, swapping and removing duplicates
Make a Python tuple, unpack it, swap two variables in one line, and use a set to remove duplicates and check membership in O(1).
-
Lesson 74 min read
Python dictionaries: keys, values, get, items and counting
Store Python values by key in a dictionary, look them up, add new ones, loop over them with items(), avoid a KeyError with get(), and count words.
-
Lesson 84 min read
Python functions: def, return, default arguments and *args
Write your own Python functions with def, return a result, use default and keyword arguments, understand local variables, and collect any number of arguments with *args.
-
Lesson 94 min read
Python list comprehensions: lists, dicts, sets, if, any and all
Build a Python list, dictionary or set from another one in a single line, filter it with if, test it with any and all, and know when a plain loop is clearer.
-
Lesson 104 min read
Python errors and exceptions: try, except, else, finally, raise
Catch a Python error with try and except instead of crashing, run cleanup with finally, raise your own errors, and read a traceback from the bottom up.
-
Lesson 114 min read
Python read and write files and JSON: open, pathlib, json.dump and json.load
Read and write files in Python with open and a with block, build paths with pathlib, and save a dictionary as JSON with json.dump and get it back with json.load.
-
Lesson 124 min read
Python modules, import and uv: the standard library, packages and pyproject.toml
Use code other people wrote with import, from and as, install packages with uv add, keep each project in its own virtual environment, and tidy it with pyproject.toml and ruff.
-
Lesson 134 min read
Python classes for beginners: __init__, self, methods and @dataclass
Build your own type with class, store data on it with __init__ and self, give it behaviour with methods, print it clearly with __repr__, and see the @dataclass shortcut.
-
Lesson 144 min read
Python to-do list project for beginners: lists, dicts and JSON
Build a small Python to-do app from what you already know: tasks as a list of dictionaries, saved to a JSON file, loaded back with try and except, and printed with enumerate.