Python Tutorials
Standalone Python tutorials built around concrete bugs, performance traps, and everyday scripting tasks. Jump straight to guides on fixing ModuleNotFoundError, avoiding mutable default arguments, reading 10 GB logs line by line, auditing rejected CSV rows, and batching requests with asyncio.gather.
Each tutorial opens with a minimal script and its exact stdout or traceback output, followed by the fix and common mistakes to avoid.
A closure remembers variables from the function that created it.
Don't guess which part is slow. Run cProfile, read cumtime and tottime the right way, fix the one line that matters, and measure again.
A for loop calls iter() once and next() until StopIteration.
If a class mostly holds data, @dataclass writes __init__, a readable printout and equality for you.
ModuleNotFoundError has a handful of causes: the wrong Python, an inactive virtual environment, the wrong folder, or a file that shadows a module.
Defaults make functions easier to call and keywords make calls easier to read. A list as a default value is created once and shared by every call. Here's why, and the one-line fix.
An if/elif chain stops at the first true condition. Put the widest one first and the others never run, with no error.
Importing a file runs it. if __name__ == "__main__" keeps a file's script part from running when another file imports it.
Python is probably installed. Your terminal just can't find it.
/, //, % and round() each do exactly what they say, and that's not always what school math taught you.
print() is for the output a person asked for. logging is for messages about the program: levels you can turn down, a file with times, and tracebacks for errors.
Start mypy with its defaults, fix what it finds, turn on check_untyped_defs, then make one module at a time strict.