Skip to content
Python Programming

Control Flow in Python

Beginner Lesson 4 of 10

Control flow statements allow you to control the execution order of your code.

age = 18
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
else:
print("Adult")
status = "adult" if age >= 18 else "minor"
# Iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# Using range
for i in range(5):
print(i) # 0, 1, 2, 3, 4
# Enumerate for index and value
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
count = 0
while count < 5:
print(count)
count += 1
# break - exit loop
for i in range(10):
if i == 5:
break
print(i)
# continue - skip iteration
for i in range(5):
if i == 2:
continue
print(i)
# else clause (runs if loop completes normally)
for i in range(5):
print(i)
else:
print("Loop completed!")

Continue to Functions →