Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is nested conditional execution in Python?
Nested conditional execution means putting one if-else statement inside another if or else block. It helps check multiple conditions step-by-step.
Click to reveal answer
beginner
How do you write a nested if statement in Python?
You write an if statement inside another if or else block, like:
if condition1:
if condition2:
# code here
Click to reveal answer
beginner
Why use nested conditional execution?
It helps check detailed conditions in order. For example, first check if a person is adult, then check if they have a driving license.
Click to reveal answer
beginner
What happens if the outer condition in nested if is False?
The inner if statements are skipped completely because the outer condition controls if the inner code runs.
Click to reveal answer
beginner
Example: What will this code print?
age = 20
if age >= 18:
if age >= 21:
print('Adult and can drink')
else:
print('Adult but cannot drink')
else:
print('Not an adult')
It will print: Adult but cannot drink because age is 20, which is >= 18 but less than 21.
Click to reveal answer
What does nested conditional execution allow you to do?
ARun code without any condition
BWrite only one condition
CSkip all conditions
DCheck multiple conditions inside each other
✗ Incorrect
Nested conditional execution lets you check one condition inside another to handle complex decisions.
In nested if statements, when does the inner if run?
AOnly if the outer if condition is True
BAlways, no matter what
COnly if the outer if condition is False
DNever
✗ Incorrect
The inner if runs only if the outer if condition is True, because it is inside that block.
What will this code print?
score = 85
if score >= 90:
print('A grade')
else:
if score >= 80:
print('B grade')
else:
print('C grade')
AA grade
BB grade
CC grade
DNo output
✗ Incorrect
Score is 85, so outer if is False, inner if is True, so it prints 'B grade'.
Which keyword is NOT used in nested conditional execution?
Afor
Belse
Celif
Dif
✗ Incorrect
'for' is a loop keyword, not used for conditional execution.
What is the main benefit of using nested conditionals?
AAvoid using else statements
BMake code run faster
CSimplify checking multiple related conditions stepwise
DWrite fewer lines of code
✗ Incorrect
Nested conditionals help check related conditions one after another clearly.
Explain nested conditional execution and give a simple example.
Think about checking one condition inside another.
You got /2 concepts.
Describe how the flow of execution works in nested if statements.
Imagine opening boxes inside boxes only if the first box is open.
You got /3 concepts.
Practice
(1/5)
1. What does nested conditional execution mean in Python?
easy
A. An if or else inside another if or else
B. Using multiple if statements one after another
C. Writing if statements without indentation
D. Using only one if statement in a program
Solution
Step 1: Understand the meaning of nested conditionals
Nested conditionals mean putting one conditional inside another, like an if inside an if.
Step 2: Compare options to definition
An if or else inside another if or else correctly describes this as an if or else inside another if or else. Other options describe different or incorrect ideas.
Final Answer:
An if or else inside another if or else -> Option A
Quick Check:
Nested conditional = if inside if [OK]
Hint: Look for conditionals inside other conditionals [OK]
Common Mistakes:
Thinking multiple separate ifs are nested
Ignoring indentation importance
Confusing nested with chained conditionals
2. Which of the following is the correct syntax for nested conditionals in Python?
easy
A. if x > 0:
if x < 10:
print('x is between 1 and 9')
B. if x > 0
if x < 10:
print('x is between 1 and 9')
C. if x > 0:
if x < 10:
print('x is between 1 and 9')
D. if x > 0:
print('x is positive')
else if x < 10:
print('x is less than 10')
Solution
Step 1: Check indentation and colons
Python requires colons after if and proper indentation for nested blocks.
Step 2: Analyze each option
if x > 0:
if x < 10:
print('x is between 1 and 9') uses colons and indents the inner if correctly. Options A and B miss colons or indentation. if x > 0:
print('x is positive')
else if x < 10:
print('x is less than 10') uses invalid else if instead of elif.
Final Answer:
if x > 0:
if x < 10:
print('x is between 1 and 9') -> Option C
Quick Check:
Colons + indentation = correct syntax [OK]
Hint: Check colons and indentation for nested blocks [OK]
Common Mistakes:
Missing colons after if statements
Incorrect indentation of nested if
Using else if instead of elif
3. What is the output of this code?
score = 85
if score >= 90:
print('Grade A')
else:
if score >= 80:
print('Grade B')
else:
print('Grade C')
medium
A. Grade A
B. Grade B
C. Grade C
D. No output
Solution
Step 1: Check first condition
score is 85, which is not >= 90, so skip first print.
Step 2: Check nested else condition
Inside else, check if score >= 80. 85 >= 80 is True, so print 'Grade B'.
4. Find the error in this nested conditional code:
num = 5
if num > 0:
if num < 10:
print('Number is between 1 and 9')
medium
A. Missing colon after first if
B. No error, code is correct
C. Using print without parentheses
D. Incorrect indentation of inner if and print
Solution
Step 1: Check colons
Both if statements have colons, so no missing colon error.
Step 2: Check indentation
Inner if and print are not indented under outer if, causing syntax error.
Final Answer:
Incorrect indentation of inner if and print -> Option D
Quick Check:
Nested blocks must be indented [OK]
Hint: Indent nested if and its code properly [OK]
Common Mistakes:
Forgetting to indent nested blocks
Confusing missing colon with indentation error
Assuming print syntax error without checking Python version
5. You want to check if a number is positive, and if positive, check if it is even or odd. Which nested conditional code correctly prints 'Positive even' or 'Positive odd' accordingly?
hard
A. if num > 0:
if num % 2 == 0:
print('Positive even')
else:
print('Positive odd')
B. if num > 0 and num % 2 == 0:
print('Positive even')
else:
print('Positive odd')
C. if num > 0:
if num % 2 == 0:
print('Positive even')
else:
print('Positive odd')
D. if num > 0:
if num % 2 == 0:
print('Positive even')
else:
print('Positive odd')
Solution
Step 1: Understand the logic needed
First check if number is positive, then inside that check if even or odd.
Step 2: Analyze options for correct nesting and indentation
if num > 0:
if num % 2 == 0:
print('Positive even')
else:
print('Positive odd') correctly nests the even/odd check inside the positive check with proper indentation. if num > 0 and num % 2 == 0:
print('Positive even')
else:
print('Positive odd') combines conditions but does not handle odd positive numbers correctly. Options C and D have indentation errors.
Final Answer:
if num > 0:
if num % 2 == 0:
print('Positive even')
else:
print('Positive odd') -> Option A
Quick Check:
Nested if with correct indentation prints right message [OK]
Hint: Indent inner if inside outer if for stepwise checks [OK]