Learn fucking Python
A free Python tutorial for people who would rather write code than read about it. Short lessons, each a handful of small programs you run yourself, explained the way a friend would explain them at 2 a.m. while your code refuses to work.
Every example ran on real Pythons, 3.9 to 3.15, and the output is pasted, not imagined. When something breaks, and it will, the second half of the site explains every error message you are about to meet.
The Python lessons, in order
- Lesson 1Hello, worldInstall the thing, print a greeting, break it on purpose. The traditional start, minus the ceremony.
- Lesson 2Variables and typesStick a name on a value, ask Python what type it is, then meet the NameError you will see a thousand times.
- Lesson 3StringsGlue text together, chop it into slices, and find out why "Age: " + 30 makes Python throw a fit.
- Lesson 4Numbers and mathSplit a pizza, round a price, and find out why 0.1 + 0.2 is a fucking mess in almost every language.
- Lesson 5if, elif, elseYour code starts making its own decisions. Bad ones, mostly, but its own.
- Lesson 6ListsOne variable, a whole pile of stuff. The first item is number zero, and you'll forget that at least twice.
- Lesson 7LoopsWrite the boring part once, let the computer do it ten thousand times. Then learn how to make it stop.
- Lesson 8FunctionsName a chunk of code, call it whenever. Also: why your function quietly handed back jack shit.
- Lesson 9DictionariesLabels instead of positions. Plus KeyError, a dict's way of asking who the hell that is supposed to be.
- Lesson 10Errors and try/exceptRead the wall of error text bottom up, catch what you expected, and let the rest crash loudly as hell.
- Lesson 11FilesYour programs finally remember things between runs. Also: which single letter shreds a file.
- Lesson 12Modules, imports and pipBorrow code from Python, from your past self and from strangers on the internet. Politely, with a venv.
- Lesson 13ClassesMake your own types, learn what the hell self is, then forget it twice, like everybody does.
When it breaks: Python error messages, explained
One page per message: the code that raises it, what it means, the fix, and how every Python from 3.9 to 3.15 words it.
- TypeError
- AttributeError
- ValueError
- SyntaxError
- IndentationError
- TabError
- NameError
- UnboundLocalError
- ModuleNotFoundError
- ImportError
- IndexError
- KeyError
- RecursionError
- pip
- Before Python starts
TypeError
TypeError: can only concatenate str (not "int") to strThe Python error 'can only concatenate str (not "int") to str' means + joined text and a number. Python never converts the number for you. Three ways to fix it.TypeError: missing 1 required positional argumentThe Python error "missing 1 required positional argument" names the parameter that got no value. When it says 'self', the class was never instantiated. The fix.TypeError: 'NoneType' object is not subscriptableThe Python error "'NoneType' object is not subscriptable" means you used [ ] on a value that is None. Where the None comes from, and the fix.TypeError: 'str' object is not callableThe Python error "'str' object is not callable" usually means a variable called str, input or a method name now holds a string. How to find it, and the fix.TypeError: takes 1 positional argument but 2 were givenThe Python error "takes 1 positional argument but 2 were given" on a method almost always means a missing self. Why Python counts one more, and the fix.TypeError: unhashable type: 'list'The Python error "unhashable type: 'list'" means a list was used as a dict key or set member. Python 3.14 words it as "cannot use 'list' as a dict key". The fix.
AttributeError
AttributeError: 'list' object has no attributeThe Python error "'list' object has no attribute" means a method from another language, like push, or a str method on a list. Python 3.15 names the fix.AttributeError: module has no attributeThe Python error "module 'random' has no attribute 'randint'" usually means your own file is named like the module. Python 3.13 says so outright. The fix.AttributeError: 'NoneType' object has no attributeThe Python error "'NoneType' object has no attribute" means a call or lookup gave you None instead of an object. The usual sources, and the fix.
ValueError
ValueError: invalid literal for int() with base 10The Python error "invalid literal for int() with base 10" means int() got text that is not a whole number, such as '12.50', '' or '1,000'. The fix for each case.ValueError: not enough values to unpackThe Python error "not enough values to unpack (expected 2, got 1)": the right side has fewer items than names on the left, usually a split with no separator.ValueError: too many values to unpackThe Python error "too many values to unpack (expected 2)" means the right side has more items than names on the left. The usual case is a loop over a dict.
SyntaxError
SyntaxError: expected ':'The Python error "expected ':'" means a line that opens a block, such as if, for, def or class, does not end with a colon. Where the colon goes, and the fix.SyntaxError: invalid syntaxThe Python error "SyntaxError: invalid syntax" marks where the parser gave up, often just after the mistake. The usual causes, and what newer Pythons say.SyntaxError: '(' was never closedThe Python error "'(' was never closed" points at a bracket with no closing partner. How to find it, and the related "does not match opening parenthesis" error.
IndentationError
IndentationError: expected an indented blockThe Python error "expected an indented block" means a line ending in a colon has no indented body under it. How to read the message, and the fix.IndentationError: unindent does not match any outer indentation levelThe Python error "unindent does not match any outer indentation level" means a line was moved back to a column no block started at. How to find it, and the fix.
TabError
NameError
UnboundLocalError
ModuleNotFoundError
ImportError
IndexError
KeyError
RecursionError
pip
Before Python starts
What is new in Python error messages
New error messages in Python 3.15What changed in Python error messages in 3.15: suggestions through inner attributes and Python names for JavaScript and Java methods. Run on 3.14 and 3.15.New error messages in Python 3.14What changed in Python error messages in 3.14: keyword typo hints, elif after else, string and prefix hints, unhashable keys, async with. Run on 3.13 and 3.14.New error messages in Python 3.13What changed in Python error messages in 3.13: colour tracebacks, a warning when your file shadows a module, keyword argument hints. Run on 3.12 and 3.13.