How Programs Think in Steps
A program behaves like a recipe you can rewind. It does not hold a big goal in its head. It follows small, exact steps in order. Each step is simple, but the chain of steps can do something useful.
Python runs your code one line at a time. When you read code, try to imagine a finger moving down the page: do this, then this, then this. If a line depends on an earlier line, the earlier line must run first.
That is why breaking a problem into tiny actions matters. Tiny actions are easy to check. When something goes wrong, you can point to the exact step where it changed.
Rule of thumb
If you cannot say what a single line does, split the problem into smaller lines.
Instructions plus state
A program is two things working together: instructions and remembered information. The instructions are your lines of code. The remembered information is the current state, meaning everything Python “knows” right now as it runs.
The most common way Python remembers state is a variable. A variable is a named box that holds a value. When Python runs x = 3, it stores 3 in the box named x. When it later runs x = x + 1, it looks up the current value in x, adds 1, then stores the new result back into x.
This is why order matters. A line can only use what exists in state at that moment. If you try to use total before you assign it, Python has no value to look up.
Watching boxes change
Seeing state change is easier with a picture. As you look at the diagram, notice how each numbered step updates one box, and later steps read from those updated boxes.
The main takeaway: each line runs once, in order, and the “boxes” keep the latest values.
Two ways to run Python
You can run Python in two common ways. A script is a file (often something.py) that Python runs top to bottom. You edit the file, then run it again to see the new result. This mindset fits longer programs because you can keep your work organized.
A REPL is an interactive prompt where you type one line and Python responds right away. REPL stands for read, evaluate, print, loop. It feels like a conversation: you ask a question like 2 + 2, Python answers 4, and you keep going. The REPL is great for quick experiments, but it is easy to forget what you already defined in that session.
If something behaves differently in a script than in the REPL, it is usually because the state is not the same.
Frame problems as input, processing, output
Before writing code, decide what goes in, what happens, and what comes out. Input is the data you start with. Processing is the step by step work you do to the data. Output is the result you want to produce.
Example: “Calculate a restaurant tip.”
- Input: bill amount, tip percent
- Processing: compute
bill * percent - Output: tip amount (and maybe total bill)
This framing keeps you from jumping into code too early. Once you know the inputs and outputs, the processing becomes a list of small steps that Python can follow.
Sign up for free
Generate custom courses on any topic — with hands-on practice, AI guidance, and visuals built in.
Already have an account?