Python Control Flow (If Statements)
Control flow structures allow your program to make decisions based on conditions. The if statement is used for this purpose.
If Statements
The if statement is used to test conditions. If the condition is True, the code inside the block runs:
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Activity
Try It Yourself!
Write a program that checks whether a number is even or odd.
Quick Quiz
Quick Quiz
- What is the purpose of the
ifstatement in Python? - What happens when the condition in an
ifstatement is False?
Answers: The if statement checks a condition and runs code if the condition is True. If the condition is False, the code inside the else block will run (if defined).