So far our programs run straight down, line by line. Control flow changes that: it lets a program make decisions (do this, not that) and repeat work (do this 100 times). These two ideas — selection and iteration — are the heart of all programming. And in Python, one thing controls it all: indentation.
1Making decisions: if, elif, else
An if statement runs a block of code only when a condition is True. Add elif (else-if) for more conditions, and else for everything that's left:
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C"):. Get the indentation wrong and you get an error or wrong behaviour.Run the playground and change marks to 95, then 70, then 40 — watch which branch runs each time.
Click Run to execute.
ifruns a block when its condition is True;elifadds more conditions;elsecatches the rest.- The condition line ends with a colon
:. - Python uses indentation (usually 4 spaces), not braces, to define a block — it must be consistent.
2Repeating with for and range()
A for loop repeats a block a known number of times. It's almost always paired with range(), which generates a sequence of numbers:
range(5)→ 0, 1, 2, 3, 4 (starts at 0, stops before 5).range(1, 6)→ 1, 2, 3, 4, 5 (start, stop).range(0, 10, 2)→ 0, 2, 4, 6, 8 (start, stop, step).
range(start, stop) stops just before stop — the stop value is never included. So range(1, 6) gives 1 to 5, not 1 to 6.The playground below prints a multiplication table — change the number 5 to build any table you like:
Click Run to execute.
- A
forloop repeats a block a known number of times, usually withrange(). range(start, stop, step); the stop value is excluded.- range(1, 6) gives 1,2,3,4,5 — not 6.
3while loops, break and continue
A while loop repeats as long as a condition stays True — use it when you don't know in advance how many times to loop:
n = 1
while n <= 5:
print(n)
n += 1 # MUST change n, or the loop never endswhile loop needs something inside it that eventually makes the condition False (here, n += 1). Forget it and the loop runs forever. (Our playground stops after 10 seconds to protect you.)Controlling a loop
break— exit the loop immediately, right now.continue— skip the rest of this turn and jump to the next iteration.
Loops can also be nested (a loop inside a loop) — useful for grids and tables. Try the playground:
Click Run to execute.
- A
whileloop repeats as long as its condition is True; something inside must eventually make it False. breakexits the loop immediately;continueskips to the next iteration.- Loops can be nested (a loop inside a loop) for grids and tables.
4Watch a loop run, step by step
It's one thing to run a loop and see its output; it's another to watch the loop variable change on every pass. Use the visualizer below: drag the start, stop and step sliders, then press Step to advance one iteration at a time (or Run all). Watch i update and the output grow — and notice again that the loop stops before the stop value.
for i in range(0, 5, 1):print(i)- On each iteration the loop variable
itakes the next value from range(). - The loop ends when range() is exhausted — the stop value is never reached.
- Stepping through one iteration at a time makes the flow of a loop concrete.
★ Project: multiplication table generator
Using the playground, build a small program that:
- Stores a number in a variable (e.g. num = 7).
- Uses a for loop with range(1, 11) to print its table from 1 to 10.
- Then add an if inside the loop that prints 'halfway!' when i is 5.
- Bonus: use a while loop to do the same table, and make sure it isn't infinite.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.