If-else helps your program make choices. It runs one set of instructions if something is true, and another set if it is not.
If–else 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 else: # code to run if condition is false
The condition is a question your program asks, which is either true or false.
Indentation (spaces before code) shows which lines belong to if or else.
Examples
age is 18 or more. If yes, it prints a message saying you can vote. Otherwise, it says you are too young.Python
if age >= 18: print("You can vote.") else: print("You are too young to vote.")
Python
if temperature > 30: print("It's hot outside.") else: print("It's not too hot today.")
Sample Program
This program checks the temperature and prints a message. Since 25 is not greater than 30, it prints the else message.
Python
temperature = 25 if temperature > 30: print("It's hot outside.") else: print("It's not too hot today.")
Important Notes
You can add more choices using elif (else if) for multiple conditions.
Always make sure your conditions cover all cases you want to handle.
Summary
If-else lets your program choose between two paths.
Use it to run code only when certain conditions are true or false.
Indentation is important to show which code belongs to each part.
Practice
1. What does an
if-else statement do in Python?easy
Solution
Step 1: Understand the purpose of if-else
An if-else statement lets the program decide which code to run based on a condition being true or false.Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code blocks, and lists store multiple items, so these are not correct.Final Answer:
It chooses between two paths based on a condition. -> Option AQuick Check:
If-else = choose path [OK]
Hint: If-else picks one of two paths based on condition [OK]
Common Mistakes:
- Confusing if-else with loops
- Thinking if-else creates data structures
- Mixing if-else with function definitions
2. Which of the following is the correct syntax for an if-else statement in Python?
easy
Solution
Step 1: Recall Python if-else syntax
Python uses a colon after the condition and indentation for the code blocks.Step 2: Check each option
if x > 0: print('Positive') else: print('Non-positive') uses colons and indentation correctly. if x > 0 then print('Positive') else print('Non-positive') uses 'then' which is not Python syntax. if (x > 0) { print('Positive'); } else { print('Non-positive'); } uses braces and semicolons, which are for other languages. if x > 0 print('Positive') else print('Non-positive') misses colons and indentation.Final Answer:
if x > 0: print('Positive') else: print('Non-positive') -> Option AQuick Check:
Colon + indent = correct if-else [OK]
Hint: Remember colons and indentation for if-else in Python [OK]
Common Mistakes:
- Using 'then' keyword
- Forgetting colons after if and else
- Not indenting code blocks
3. What will be the output of this code?
age = 18
if age >= 18:
print('Adult')
else:
print('Minor')medium
Solution
Step 1: Evaluate the condition age >= 18
Since age is 18, the condition age >= 18 is true.Step 2: Determine which block runs
Because the condition is true, the code inside the if block runs, printing 'Adult'.Final Answer:
Adult -> Option BQuick Check:
Condition true -> print 'Adult' [OK]
Hint: Check if condition is true or false to pick output [OK]
Common Mistakes:
- Assuming >= means less than
- Printing else block by mistake
- Confusing output with variable value
4. Find the error in this code:
num = 5
if num > 0
print('Positive')
else:
print('Non-positive')medium
Solution
Step 1: Check syntax of if statement
The if statement must end with a colon ':' to mark the start of the block.Step 2: Identify the missing colon
The code misses the colon after 'if num > 0', causing a syntax error.Final Answer:
Missing colon ':' after 'if num > 0' line -> Option CQuick Check:
Colon needed after if condition [OK]
Hint: Always put colon after if condition [OK]
Common Mistakes:
- Forgetting colon after if
- Confusing else with elif
- Incorrect indentation
5. You want to write a program that prints 'Even' if a number is even, and 'Odd' if it is odd. Which code correctly uses if-else to do this?
hard
Solution
Step 1: Understand how to check even numbers
A number is even if dividing by 2 leaves no remainder, so num % 2 == 0 is true for even numbers.Step 2: Check each option's condition
num = 4 if num % 2 == 0: print('Even') else: print('Odd') correctly uses num % 2 == 0. num = 4 if num / 2 == 0: print('Even') else: print('Odd') uses division instead of modulo, which is wrong. num = 4 if num % 2: print('Even') else: print('Odd') treats nonzero remainder as even, which is incorrect. num = 4 if num % 2 != 0: print('Even') else: print('Odd') reverses the logic.Final Answer:
num = 4 if num % 2 == 0: print('Even') else: print('Odd') -> Option DQuick Check:
Modulo equals zero means even [OK]
Hint: Use 'num % 2 == 0' to check even numbers [OK]
Common Mistakes:
- Using division instead of modulo
- Reversing even and odd logic
- Checking truthiness of modulo without comparison
