Conditionals and Loops
Master conditional logic and loop iteration over ranges and sequences.
Learning objectives
- Use if, elif, and else blocks with comparison (==, !=, >, <) and logical operators (and, or, not)
- Iterate over sequences with for loops and range()
- Control loop execution with break and continue
Lesson material
For Loops and range()
range(start, stop, step) generates integers up to (but excluding) stop.
Example code
# Print even numbers from 0 to 8
for i in range(0, 10, 2):
print(i, end=" ")
print()
Practice exercise: Sum of Odd Numbers
Write a loop that calculates the sum of all odd numbers from 1 to 9 inclusive and prints the total sum.
Test yourself with the Module 5: Control Flow & Loops quiz →