Python Lesson 9: Control Flow
PYTHON
AllComputerss
4/18/20261 min read


Control flow refers to the order in which individual statements, instructions, or functions are executed in a program. In Python, control flow is managed using conditional statements and loops, allowing you to make decisions and repeat actions based on certain conditions.
Conditional Statements
Conditional statements let you execute code only if certain conditions are met.
Example:
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
if: Executes a block of code if the condition is true.
elif: Checks another condition if the previous one was false.
else: Executes if none of the conditions are true.
Nested Conditions
You can nest conditions to check multiple layers of logic:
age = 20
if age >= 18:
if age < 21:
print("You are an adult but not yet 21.")
else:
print("You are 21 or older.")
else:
print("You are under 18.")
Loops
Loops allow you to repeat actions until a condition is met.
For Loop
Used to iterate over a sequence (like a list or string):
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
While Loop
Repeats as long as a condition is true:
count = 0
while count < 5:
print("Count is:", count)
count += 1
Break and Continue
break: Exits the loop entirely.
continue: Skips the current iteration and moves to the next.
for i in range(5):
if i == 3:
break
print(i)
for i in range(5):
if i == 2:
continue
print(i)
Match/Case (Python 3.10+)
Python introduced a match statement for pattern matching:
status = 200
match status:
case 200:
print("OK")
case 404:
print("Not Found")
case _:
print("Unknown status")
Conclusion
Control flow is essential for writing programs that can make decisions and perform repetitive tasks. By mastering conditional statements, loops, and pattern matching, you’ll gain the ability to build dynamic and responsive Python applications.
© 2026 AllComputerss. All rights reserved.