You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Control flow lets your program make decisions. Instead of running every line from top to bottom, your code can choose different paths based on conditions. The if, elif, and else statements are the foundation of decision-making in Python.
if StatementThe simplest form of a conditional — run a block of code only if a condition is True:
age = 18
if age >= 18:
print("You are an adult.")
print("You can vote.")
print("This always runs.")
Output:
You are an adult.
You can vote.
This always runs.
Key syntax:
:if...elseProvide an alternative path when the condition is False:
temperature = 35
if temperature > 30:
print("It's hot outside!")
else:
print("The weather is pleasant.")
if...elif...elseUse elif (short for "else if") for multiple conditions:
score = 75
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.