If–else execution flow in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to see how the time a program takes changes when it uses if-else decisions.
How does choosing one path or another affect how long the program runs?
Analyze the time complexity of the following code snippet.
def check_number(num):
if num > 0:
return "Positive"
else:
return "Non-positive"
result = check_number(10)
This code checks if a number is positive or not and returns a message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else decision.
- How many times: Exactly once per function call.
Whether the input number is small or large, the program only checks one condition once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the program takes the same amount of time no matter what number you give it.
[X] Wrong: "If-else makes the program slower as numbers get bigger."
[OK] Correct: The if-else only checks once, so bigger numbers don't add more work.
Understanding simple if-else time helps you explain how decisions affect program speed clearly and confidently.
"What if we added a loop that runs n times inside the if block? How would the time complexity change?"
Practice
if-else statement do in Python?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]
- Confusing if-else with loops
- Thinking if-else creates data structures
- Mixing if-else with function definitions
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]
- Using 'then' keyword
- Forgetting colons after if and else
- Not indenting code blocks
age = 18
if age >= 18:
print('Adult')
else:
print('Minor')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]
- Assuming >= means less than
- Printing else block by mistake
- Confusing output with variable value
num = 5
if num > 0
print('Positive')
else:
print('Non-positive')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]
- Forgetting colon after if
- Confusing else with elif
- Incorrect indentation
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]
- Using division instead of modulo
- Reversing even and odd logic
- Checking truthiness of modulo without comparison
