Nested conditional execution in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use nested conditional statements, we want to know how the program's steps grow as input changes.
We ask: Does adding more input make the program take longer, and how much longer?
Analyze the time complexity of the following code snippet.
def check_number(n):
if n > 0:
if n % 2 == 0:
return "Positive even"
else:
return "Positive odd"
else:
return "Non-positive"
This code checks if a number is positive and then if it is even or odd.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated steps here.
- How many times: The conditions run once per function call.
Since the code only checks conditions once, the steps do not increase with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 checks |
| 100 | 3 checks |
| 1000 | 3 checks |
Pattern observation: The number of steps stays the same no matter the input size.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input is.
[X] Wrong: "Nested conditions always make the program slower as input grows."
[OK] Correct: Nested conditions just check more things in order, but they don't repeat steps based on input size.
Understanding how nested conditions affect time helps you explain your code clearly and shows you know when programs stay fast.
"What if we added a loop inside the nested condition? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of nested conditionals
Nested conditionals mean putting one conditional inside another, like anifinside anif.Step 2: Compare options to definition
Aniforelseinside anotheriforelsecorrectly describes this as aniforelseinside anotheriforelse. Other options describe different or incorrect ideas.Final Answer:
Aniforelseinside anotheriforelse-> Option AQuick Check:
Nested conditional =ifinsideif[OK]
- Thinking multiple separate ifs are nested
- Ignoring indentation importance
- Confusing nested with chained conditionals
Solution
Step 1: Check indentation and colons
Python requires colons afterifand 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 innerifcorrectly. 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 invalidelse ifinstead ofelif.Final Answer:
if x > 0: if x < 10: print('x is between 1 and 9') -> Option CQuick Check:
Colons + indentation = correct syntax [OK]
- Missing colons after if statements
- Incorrect indentation of nested if
- Using else if instead of elif
score = 85
if score >= 90:
print('Grade A')
else:
if score >= 80:
print('Grade B')
else:
print('Grade C')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'.Final Answer:
Grade B -> Option BQuick Check:
85 >= 80 triggers nested if [OK]
- Assuming first if is true for 85
- Ignoring nested else block
- Confusing indentation levels
num = 5
if num > 0:
if num < 10:
print('Number is between 1 and 9')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 DQuick Check:
Nested blocks must be indented [OK]
- Forgetting to indent nested blocks
- Confusing missing colon with indentation error
- Assuming print syntax error without checking Python version
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 AQuick Check:
Nested if with correct indentation prints right message [OK]
- Combining conditions incorrectly
- Indentation errors in nested blocks
- Missing else for odd case
