Control Flow
Learn how to make your Python programs make decisions and repeat tasks so they can process data in smarter ways.
Skill Level: Beginner
Prerequisites: Variables, Data Types, Collections
Estimated Time: 30 minutes
Story Time
So far, you have learned how to store values, work with different data types, and use collections such as lists. Now you are ready for something more powerful: telling Python how to make decisions and repeat tasks.
Imagine you are working with student exam scores. You may want to check whether a score is above a target, print only selected values, or repeat the same task for every student in a list. Control flow gives your program that ability.
What You’ll Learn
By the end of this lesson, you will be able to:
- Explain what control flow means in Python.
- Use comparison operators to compare values.
- Write
if,elif, andelsestatements. - Use
forloops to repeat tasks over a sequence. - Use
whileloops to repeat tasks while a condition is true. - Use
breakandcontinueto control loop behavior.
Comparison Operators
Comparison operators compare two values and return a Boolean value: either True or False.
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | 5 == 5 |
!= | Not equal to | 5 != 3 |
> | Greater than | 10 > 4 |
< | Less than | 8 < 12 |
>= | Greater than or equal to | 18 >= 18 |
<= | Less than or equal to | 7 <= 9 |
Example
age = 20
print(age >= 18)
Output:
True
Conditional Statements
Conditional statements allow a program to choose what to do based on a condition.
The if Statement
Use if when you want code to run only when a condition is true.
age = 20
if age >= 18:
print("Eligible to vote")
Output:
Eligible to vote
Indentation matters in Python. The indented line belongs to the if block.
The if...else Statement
Use else when you want one action if the condition is true and another action if it is false.
age = 16
if age >= 18:
print("Eligible")
else:
print("Not eligible")
Output:
Not eligible
The if...elif...else Statement
Use elif when you need to check multiple conditions in order. The word elif is short for “else if”.
marks = 84
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")
Output:
Grade B
Repeating Tasks with Loops
Loops repeat a block of code. This is useful when you want to perform the same action many times.
Instead of writing this:
print("Python")
print("Python")
print("Python")
you can use a loop.
The for Loop
A for loop is useful when you want to go through a sequence, such as a list or a range of numbers.
for i in range(5):
print(i)
Output:
0
1
2
3
4
Looping Through a List
students = ["Emma", "Alex", "Sophia"]
for student in students:
print(student)
Output:
Emma
Alex
Sophia
Use a for loop when you already know the collection or sequence you want to move through.
The while Loop
A while loop repeats as long as a condition stays true.
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
Use a while loop when repetition depends on a condition rather than a fixed list or range.
Using break
The break statement stops a loop immediately, even if the loop has more work to do.
for number in range(10):
if number == 5:
break
print(number)
Output:
0
1
2
3
4
Using continue
The continue statement skips the current iteration and moves to the next one.
for number in range(6):
if number == 3:
continue
print(number)
Output:
0
1
2
4
5
Worked Example
Suppose you have a list of student math scores and you want to display only scores above 80.
scores =
for score in scores:
if score > 80:
print(score)
Output:
82
91
88
This is a simple example of how control flow supports data analysis. Your program repeats a task for every value in the list and applies a condition to decide what to display.
Practice in the Notebook
After completing the activity in Google Colab, mark it as complete below.
Try this in Google Colab:
scores =
Complete these tasks:
- Display every score.
- Display only scores greater than 80.
- Count how many scores are greater than 80.
One possible starting point:
scores =
count_above_80 = 0
for score in scores:
print(score)
for score in scores:
if score > 80:
print(score)
count_above_80 += 1
print("Number of scores above 80:", count_above_80)
Challenge
Imagine each value in the list below is a student’s reading score.
reading_scores =
Write a program that:
- Displays only the scores greater than or equal to 80.
- Counts how many scores are greater than or equal to 80.
- Prints
"Strong performance"if the count is 3 or more, otherwise prints"Needs more review".
Common Mistakes
Forgetting indentation
Incorrect:
if age >= 18:
print("Eligible")
Correct:
if age >= 18:
print("Eligible")
Using = instead of ==
Incorrect:
if age = 18:
Correct:
if age == 18:
Infinite while loops
Incorrect:
count = 1
while count <= 5:
print(count)
This loop never ends because count never changes.
Correct:
count = 1
while count <= 5:
print(count)
count += 1
Key Takeaways
- Comparison operators return
TrueorFalse. if,elif, andelsehelp programs make decisions.forloops are useful for iterating over sequences.whileloops repeat while a condition remains true.breakstops a loop, andcontinueskips to the next iteration.
Self Evaluation
Check Your Understanding
1. What does a comparison operator return in Python?
2. Which statement is used to check multiple conditions in sequence?
3. Which loop is best when you want to go through every item in a list?
4. What does break do inside a loop?
5. What does continue do inside a loop?
Continue Your Journey
Next, you will learn about Functions, which help you organize code into reusable blocks.