Python, from your first line to your first library
CodeWithPurpose lessons that turn Python into a language you actually think in, not one you look up. No setup required.
Ready when you are
Go at your own pace. Your progress saves on this device.
Syllabus
What you'll learn
Take it one chapter at a time. A short quick check at the end of each one opens the next.
Part 1
Foundations
What Python actually is, the one thing you need to run it, and what a name really points at.
- Why Python, SpecificallyEvery language claims to be simple, right up until you try to read one. Look at a working Python program you have never seen before, and notice how much of it you already understand.
- Setting Up Without the CeremonyThe usual first step in a Python tutorial is an hour of installer screenshots. Run one line without installing anything, and see the same interpreter everyone else is using.
- Names Are Not BoxesMost explanations call a variable a box that holds a value, and that picture breaks the moment one name affects another. Point two names at the same list, change it through one, and watch the other see it too.
- Arithmetic, and Where It Surprises YouTwo slashes and one slash look like a typo of each other, and they hand back different types on purpose. Divide the same two numbers both ways and see exactly where a float appears uninvited.
Part 2
Working With Data
Strings, lists, tuples, dictionaries, and sets — the shapes almost every program is built from.
- Text Is Not a Single ThingA string looks like one piece of text until you need its third character. Slice a sentence apart by position and find out why the end index is never the one you first guess.
- Ordered Collections, Two WaysPython gives you two ways to hold an ordered group of things, and the difference is not the brackets. Try to change a tuple the way you would a list, and read exactly what it refuses, and why.
- Looking Things Up Instead of Counting AlongA list makes you remember where something is. A dictionary lets you forget. Build one from scratch, look something up by name instead of position, and ask for a key that was never there.
- What Counts as Empty, False, or EqualPython will evaluate almost anything as true or false, including things that are neither. Feed in an empty list, the number zero, and the string that spells the word False, and see which ones the language actually treats as false.
Part 3
Controlling the Flow
Making a program choose, repeat, and remember work as a function instead of a copy-paste.
- Teaching a Program to ChooseA program that always does the same thing is not a program, it is a constant. Feed one input through a chain of conditions and watch exactly one branch of it ever run.
- Doing Something More Than OnceLooping in Python means iterating over the thing itself, not counting up to its length and hoping you stop in time. Step through a collection one item at a time and watch the loop variable outlive the loop that created it.
- Naming a Piece of WorkCopy the same five lines into three places in a program and you have created three bugs waiting to happen, not three features. Wrap them in a function once, and watch every copy become a single name you can call.
- Where a Name Actually LivesA variable created inside a function looks like it should be visible everywhere once the function has run. Try to read it from outside, and find out Python disagrees, on purpose.
Part 4
Thinking in Objects
Reading your own mistakes, splitting a program into files, and bundling data with the behaviour that acts on it.
- When Things Go Wrong on PurposeA traceback looks like the program's way of failing at you. Read one from the bottom up instead of the top down, and it turns into the most specific bug report you will ever get for free.
- Splitting a Program Into PiecesA single file works exactly until it does not, somewhere around the point it holds three unrelated things. Split one program across two files, import one from the other, and watch the boundary hold.
- Bundling Data With BehaviourA dictionary can hold a name and a grade, but nothing stops you misspelling the key next time you use it. Define a class once, stamp out three objects from it, and give each its own values without repeating the shape.
- Reusing Behaviour Without Copying ItTwo classes that share eighty percent of their behaviour usually end up as two copies of that eighty percent. Write the shared part once, inherit it in both, and override only what actually differs.
Part 5
Working Like a Real Program
Touching the outside world through files, writing collections in one line, and producing values lazily.
- Reading and Writing Outside the ProgramEverything so far has lived inside the program and vanished the moment it stopped running. Write a line to an actual file on disk, close it properly, and read the same line back in a program that starts fresh.
- Building a Collection in One LineBuilding a new list from an old one usually starts as three lines: an empty list, a loop, and an append. Write the same transformation as one line, and read it back exactly as fast as you wrote it.
- Producing Values One at a TimeA list built to hold a million numbers holds all million in memory at once, whether you need them yet or not. Write a generator instead, pull one value at a time, and watch it produce the next number only when asked.
- Wrapping a Function Without Rewriting ItAdding logging to ten functions usually means editing ten functions. Write the logging once as a decorator, apply it with one line above each function, and leave the original code untouched.
Part 6
Data That Has Shape
Real data arrives nested, duplicated, and missing the key you asked for — the structures and habits that survive it.
- Sorting, Slicing, and the Methods That Return NothingSorting a list of names is one function call, right up until the names have capital letters in them and the answer is quietly wrong. Sort the same list four ways, changing only the key, and watch the order rearrange itself under each rule.
- A Row Is Just a DictionaryA spreadsheet has rows and columns; Python has neither, and still holds the same data more honestly. Build a table out of nothing but a list and some dictionaries, then filter it down to three rows without ever writing a counter.
- Structures Inside StructuresEvery real API response is a dictionary holding lists holding more dictionaries, and the error for guessing wrong is four words long. Click down through a nested structure one level at a time and watch the access path build itself as you go.
- Why Changing One Changed the OtherYou copied the list, changed the copy, and the original changed with it. Point three different kinds of copy at the same nested list, mutate one item at the bottom, and see precisely which of them flinch.
- The Errors Your Data Actually ThrowsThree exceptions account for almost every crash involving real data, and each one names the exact thing that was not there. Ask a dictionary for a key it has never heard of, four different ways, and compare what each one hands back.
- The Structures Python Ships But Never AdvertisesCounting how often each word appears takes four lines and one conditional that everybody eventually gets wrong. Swap in the structure built for exactly that job and watch the same tally come out of a single line.
- Picking One, and What It CostsA list and a set both answer whether they contain something, and on ten thousand items one of them does ten thousand times more work to say so. Slide the collection size up and watch the number of checks each one performs pull apart.
Part 7
Going Further
Other people's code, the internet's data format, proving your program works, and building one from scratch.
- Standing on Other People's CodeNobody writes date parsing or HTTP requests from scratch, because somebody has already written it better and tested it more. Install a real package, import it, and use in three lines what would take an afternoon to build.
- Talking to the Outside WorldJSON looks almost exactly like a Python dictionary, right up until you notice the differences. Convert one into the other in both directions, and find the two places the conversion is not quite what you expected.
- Proving It Works, Not Just Believing ItRunning a program and reading the output by eye works fine until it has more than one path through it. Write an assertion that checks the answer for you, and let it fail loudly the moment the code stops agreeing with itself.
- Building Something From All of ItEvery chapter until now has proven one idea in isolation. Build one program that needs variables, a loop, a function, and a dictionary all in the same twenty lines, and watch them stop being separate topics.
Open source
Your chapter goes here
Every chapter here was written by a student. Propose your own topic, or fix one that confused you. Drafts stay invisible until they are ready, so nothing you start can break the site.
Keep building your Python practice
These lessons are part of CodeWithPurpose's free learning library, built by students, for students, everywhere.