Python: Control Flow
What you will learn
How to make decisions with if/elif/else, and how to repeat actions with for and while loops.
Conditionals
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "F"
print(grade) # B
Python uses indentation (4 spaces) to define blocks — no braces. The elif is short for "else if".
for loops
for fruit in ["apple", "banana", "cherry"]:
print(f"I like {fruit}s")
for i in range(5):
print(i) # 0, 1, 2, 3, 4
range(n) generates 0, 1, ..., n-1. range(start, stop, step) gives more control.
while loops
count = 0
while count < 3:
print(count)
count += 1 # 0, 1, 2
Common mistakes
- Forgetting the colon at the end of
if,for,while. - Mixing spaces and tabs (use 4 spaces consistently).
- Infinite loops: make sure your
whilecondition eventually becomesFalse.
Quick check below!