An if statement helps your program decide what to do based on a condition. It lets your code choose different paths like making a decision in real life.
If statement execution flow in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
if condition: # code to run if condition is True elif another_condition: # code to run if another_condition is True else: # code to run if none of the above conditions are True
The if line checks a condition that is either True or False.
The elif and else parts are optional and help check more conditions or run code if none match.
Examples
Python
if temperature > 30: print("It's hot outside!")
Python
if score >= 90: print("You got an A") elif score >= 80: print("You got a B") else: print("Keep trying!")
Python
if is_raining: print("Take an umbrella") else: print("No umbrella needed")
Sample Program
This program checks the temperature and prints a message based on its value.
Python
temperature = 25 if temperature > 30: print("It's hot outside!") elif temperature > 20: print("The weather is nice.") else: print("It's a bit cold.")
Important Notes
Indentation (spaces before code) is very important in Python to show which code belongs to the if or else.
Only one block of code runs in an if-elif-else chain, the first condition that is True.
If no condition is True and there is no else, nothing happens.
Summary
If statements let your program make choices based on conditions.
Use if, elif, and else to check multiple conditions.
Indent your code properly to show which parts belong to each condition.
Practice
1. What does an
if statement do in Python?easy
Solution
Step 1: Understand the purpose of
Anififstatement checks a condition and runs code only if that condition is true.Step 2: Compare with other options
Repeating code is done by loops, storing data is done by lists, and defining functions usesdef.Final Answer:
It runs code only if a condition is true. -> Option CQuick Check:
ifruns code if condition true [OK]
Hint: If checks condition; runs code only when true [OK]
Common Mistakes:
- Confusing if with loops
- Thinking if stores data
- Mixing if with function definition
2. Which of the following is the correct syntax for an
if statement in Python?easy
Solution
Step 1: Recall Python
Python uses a colonifsyntax:after the condition and no parentheses or 'then'.Step 2: Check each option
if x > 5: usesif x > 5:which is correct. Others use 'then' or braces which are not Python syntax.Final Answer:
if x > 5: -> Option BQuick Check:
Pythonifends with colon [OK]
Hint: Python
if ends with colon, no 'then' or braces [OK]Common Mistakes:
- Adding 'then' after condition
- Using braces {} like other languages
- Forgetting the colon at the end
3. What will be the output of this code?
age = 20
if age < 18:
print("Child")
elif age < 65:
print("Adult")
else:
print("Senior")medium
Solution
Step 1: Check the value of
The variableageageis 20.Step 2: Evaluate conditions in order
First conditionage < 18is false (20 is not less than 18). Second conditionage < 65is true (20 is less than 65), so it prints "Adult" and skips theelse.Final Answer:
Adult -> Option AQuick Check:
20 is less than 65, prints Adult [OK]
Hint: Check conditions top to bottom; first true runs [OK]
Common Mistakes:
- Printing Child for age 20
- Ignoring
elifand jumping to else - Thinking no output if first condition false
4. Find the error in this code:
score = 75
if score >= 90
print("Excellent")
elif score >= 60:
print("Pass")
else:
print("Fail")medium
Solution
Step 1: Check syntax of
The firstifstatementifline is missing a colon:at the end, which is required in Python.Step 2: Verify other parts
Indentation andelifusage are correct. The variablescoreis defined.Final Answer:
Missing colon after first if condition -> Option DQuick Check:
Everyifneeds a colon [OK]
Hint: Check colons after all if/elif/else lines [OK]
Common Mistakes:
- Forgetting colon after if condition
- Confusing elif with else if syntax
- Incorrect indentation of print lines
5. You want to print "Positive", "Zero", or "Negative" based on a number's value. Which code correctly uses
if, elif, and else to do this?hard
Solution
Step 1: Understand correct
Useif-elif-elsestructureiffor first condition,eliffor the second, andelsefor all other cases.Step 2: Check each option
if num > 0: print("Positive") elif num == 0: print("Zero") else: print("Negative") correctly useselifandelse. if num > 0: print("Positive") if num == 0: print("Zero") else: print("Negative") uses two separateifstatements which can cause multiple prints. if num > 0: print("Positive") else if num == 0: print("Zero") else: print("Negative") uses invalid syntaxelse if. if num > 0: print("Positive") elif num < 0: print("Zero") else: print("Negative")'selif num < 0will print "Zero" for negative numbers and "Negative" for zero incorrectly.Final Answer:
if num > 0: print("Positive") elif num == 0: print("Zero") else: print("Negative") -> Option AQuick Check:
Use if, elif, else for exclusive conditions [OK]
Hint: Use if, elif, else for clear exclusive choices [OK]
Common Mistakes:
- Using multiple separate ifs causing multiple outputs
- Writing else if instead of elif
- Overlapping conditions causing wrong output
