Phase 02 · Python FoundationsLesson 3 of 8
Conditions & Loops
Make decisions with if/elif/else and repeat work with for and while loops.
The problem
So far your programs run straight down, line by line. Real programs need to make decisions ("is this user logged in?") and repeat work ("process every row in this file"). That's what control flow gives you: conditions and loops.
The concept
Making decisions with if#
score = 72
if score >= 90:
grade = "A"
elif score >= 70:
grade = "B"
else:
grade = "C"
print(grade) # B
- Python checks each condition top to bottom and runs the first block that's true.
elif("else if") adds more checks;elsecatches everything left over.- Indentation defines the block. Four spaces is the standard.
How Python picks a block
score = 72- Check conditions in order
score >= 90grade = "A"
elif score >= 70grade = "B"runs here, so the result is B
elsegrade = "C"catches everything left over
You can combine conditions with and, or and not:
if age >= 18 and has_id:
print("Entry allowed")
Repeating with for#
A for loop runs once for each item in a sequence:
tools = ["Python", "SQL", "Git"]
for tool in tools:
print(f"Learning {tool}")
To repeat something a set number of times, use range():
for i in range(1, 6):
print(i) # 1 2 3 4 5 (the stop value 6 is excluded)
range(0, 10, 2) # 0, 2, 4, 6, 8
Repeating with while#
A while loop repeats as long as a condition stays true. Use it when you don't know in advance how many repetitions you'll need:
attempts = 0
while attempts < 3:
print("Trying to connect…")
attempts += 1
A while loop
- Check
attempts < 3repeat only while it stays true - Print "Trying to connect…"
attempts += 1moves the loop toward stopping
Stops once attempts < 3 is false
Controlling loops: break and continue#
for n in range(1, 10):
if n == 3:
continue # skip 3, go straight to the next number
if n == 6:
break # stop the loop completely
print(n) # prints 1 2 4 5
breakexits the loop immediately.continueskips the rest of the current iteration.passdoes nothing; it's a placeholder when Python requires a block but you have nothing to write yet.
Putting it together: FizzBuzz#
A classic interview warm-up that combines both ideas:
for n in range(1, 21):
if n % 3 == 0 and n % 5 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)
Notice the order: the "both" case must come first, otherwise 15 would match "divisible by 3" and print "Fizz". Order of conditions is a common source of subtle bugs.
Try it
Your task · about 10 minutes
Write FizzBuzz for 1–20: print "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for both, otherwise the number.
Recap & next
Key takeaways
if,elifandelsechoose which block runs; indentation defines each block.forloops iterate over any sequence: lists, strings, ranges.range(start, stop, step)generates numbers up to, but not including,stop.whilerepeats as long as its condition stays true. Make sure the condition eventually becomes false.breakexits a loop immediately;continueskips to the next iteration.
Quick check
2 questions · instant feedback
Go deeper
Finished this lesson?
Mark it complete to track your progress and keep your streak going.



