Variables, Types, Control Flow & Functions
These are the four building blocks you'll use in every single problem — names, types, decisions, and reusable tools. Get comfy here and everything after gets easier.
The idea, in one line
You store values under names, values come in a few types, if and loops decide which lines run, and functions let you reuse a chunk of logic. That's the whole toolkit.
Variables: names, not boxes
A variable is just a name that points at a value. When you write status = 200, the = sign sticks the name status onto the value 200. Python doesn't copy anything — it just points a label at it.
You can move that label whenever you like. status = 200, then later status = "OK" is fine — the name now points somewhere new.
The four everyday types
A type is just what kind of value you have. Four of them cover almost everything:
int— whole numbers, like a status code 200 or a retry countfloat— decimals, like a 1.6s response timestr— text, like a log line or a URLbool— True or False, e.g. did the test pass?
You do maths with + - * /. Three operators come up a lot in testing:
/always gives a decimal:10 / 2is5.0//divides down to a whole number:10 // 3is3%gives the leftover — handy for "is this number even?" withn % 2 == 0
Control flow: choosing which lines run
Control flow just means deciding which lines run and how often. if / elif / else picks a branch based on a condition. A for loop walks through a list or string one item at a time. range(n) hands you 0 up to n-1 when you only need a counter. A while loop repeats until its condition turns False.
Inside any loop, break stops it early and continue jumps straight to the next item.
Functions: your reusable tools
A function wraps a chunk of logic behind a name using def. You feed it parameters, it does its work, and it hands back an answer with return. No return? Then it gives back None. Parameters can have defaults, so callers may skip them.
See it work
# A tiny QA helper: label an HTTP status code
def classify(status):
if status < 300:
return "pass"
elif status < 500:
return "client-error"
else:
return "server-error"
# Walk a batch of results from a test run
results = [200, 404, 200, 503, 301]
passes = 0
for code in results:
label = classify(code)
if label == "pass":
passes += 1
print(code, "->", label)
print(f"{passes}/{len(results)} passed")Read it top to bottom: classify turns one code into a label, then the loop runs it on every code and counts the passes. Names, a type check, control flow, and a function — all four blocks in one small script.
Advanced — why the mental model matters
Because a name only points at a value, two names can point at the same list. Change it through one name and the other name sees the change too. It surprises people, so it's worth knowing early. Small, well-named functions keep this kind of surprise contained — each tool touches only what it's handed.
Grounded in the official Python tutorial (Introduction & Control Flow)
All lessons in Python Foundations & The 6-Step Method
- Variables, Types, Control Flow & Functions
- Lists, Dicts, Sets & Tuples — When and Why
- String Manipulation Patterns
- The 6-Step Method — A Worked Example
- Hand-Tracing — Where Logic Actually Grows