Technology & Computing · Software & algorithms
How programming works
Most programs are built from a handful of ideas. See how variables, decisions, loops and functions fit together, and learn to read a short program line by line. No experience needed.

What you will learn
- Explain what a variable is and what an assignment like x = x + 1 does
- Explain why a program's lines run one at a time in order, and why the order changes the result
- Explain how an if statement chooses which lines run by testing a condition
- Explain how a loop repeats lines, and why something inside it must change for it to stop
Lesson 1 of 4 · 4 min
How a program remembers and changes values
In math class, x = x + 1 is nonsense, because no number equals itself plus one. In a program it's one of the most common lines there is.
A variable is a name attached to a stored value. The line "price = 4" is an assignment. It tells the computer to store 4 under the name price. Picture a labeled box holding a value. In Python the name works more like a tag tied to the value, but the box picture is fine for reading code.
So "x = x + 1" runs in two steps. First the computer works out the right side, using the value x holds now. Then it stores the result back in x. If x was 5, it's now 6. Programmers call this an update.
A program runs one statement, or instruction, at a time, from top to bottom. Each line sees the values left by the lines before it, so order matters.
Say a holds 1, b holds 2, and you want to swap them. You write "a = b" and then "b = a". After the first line, a holds 2. The second line copies a's new value into b, so both hold 2. The 1 is gone.
The fix uses a third variable to keep one value safe. Write "temp = a", then "a = b", then "b = temp". Now a holds 2 and b holds 1.
A later line never reaches back. If "y = x + 1" runs when x is 2, y holds 3, and it stays 3 even if x changes afterward.
Try to recallA program has a = 1 and b = 2, then runs a = b followed by b = a. Why doesn't that swap the two values?
The lines run in order. After "a = b", a holds 2 and the original 1 is gone. Then "b = a" copies that 2 into b, so both hold 2. A third variable has to keep one value while you move the other.
A variable is a name for a stored value, and = stores the right side under the left name. The lines run in order, so you can now predict a short program by following its values one line at a time.
Lessons in this topic
How a program remembers and changes values
See what a variable is, why "x = x + 1" makes sense in code, and why the order of lines decides what a program does.
How a few lines handle thousands of cases
Learn how if statements choose which lines run and how loops repeat them, and why those two ideas, with lines in order, are enough to build any program.
Why programs are built from small named functions
See how a function packages lines under a name, takes inputs and hands back a result, and why programmers build everything from small ones.
Reading a program you didn't write
Trace a short program on paper to predict exactly what it prints, and learn to spot two traps that catch beginners and professionals alike.
Three quick stories from this topic
The equals sign debate
Niklaus Wirth called using = to store values in code a notorious bad idea
In math, = says two sides are equal, either way round. Fortran popularized it in 1957 to mean "make the left side equal the right," so x = y and y = x do different things. Wirth said language designers copied it blindly.
Older than Fortran
The habit goes back to Heinz Rutishauser's language Superplan, designed between 1949 and 1951.
Two signs for two jobs
C tests equality with ==. ALGOL and Pascal kept = for equality and assign with := instead.
The first program's bug
The program often called the first ever had a bug, two values swapped in a division
Note G, published in 1843 with Ada Lovelace's notes on Babbage's Analytical Engine, laid out steps for computing Bernoulli numbers. The machine was never built, so nobody ran it. Modern tracing found one division done the wrong way round.
A wrong answer
With the swap, the steps would have produced -25621/630 instead of the eighth Bernoulli number, -1/30.
She caught one too
Babbage recalled in 1864 that Lovelace sent back his own working on the Bernoulli numbers after finding a grave mistake in it.
Three structures for everything
Two computer scientists proved in 1966 that three structures can build any program
Corrado Böhm and Giuseppe Jacopini showed that steps in order, choices between two paths and repetition can compute anything a computer can. Every program can be written with just those three.
Possible is not pretty
Their method of rewriting a tangled program often made it harder to read, so the proof started a debate rather than settling it.
The goto letter
Edsger Dijkstra's famous letter "Go To Statement Considered Harmful" followed in 1968.
Sources
- Think Python, chapter 2: Variables, expressions and statements, Allen B. Downey, Think Python, 2nd edition (Green Tea Press)
- Think Python, chapter 7: Iteration, Allen B. Downey, Think Python, 2nd edition (Green Tea Press)
- Variable (high-level programming language), Wikipedia
- An Informal Introduction to Python, Python Software Foundation
- Assignment (computer science), Wikipedia
- Think Python, chapter 3: Functions, Allen B. Downey, Think Python, 2nd edition (Green Tea Press)
- Note G, Wikipedia
- Structured program theorem, Wikipedia




