Assert statement usage in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's see how using assert statements affects the time a program takes to run.
We want to know how the program's speed changes as the input grows when asserts are used.
Analyze the time complexity of the following code snippet.
def check_positive(numbers):
for num in numbers:
assert num > 0, f"Number {num} is not positive"
return True
This code checks if all numbers in a list are positive using assert statements inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the list and checking with assert.
- How many times: Once for each number in the input list.
Each number in the list is checked one by one, so the work grows as the list grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items checked.
[X] Wrong: "Assert statements make the program run instantly or take no time."
[OK] Correct: Assert statements still run each time they are reached, so they add to the total work, especially inside loops.
Understanding how assert statements affect time helps you write clear and efficient checks in your code, a skill useful in many programming tasks.
"What if we replaced the assert with a simple if-statement that raises an error? How would the time complexity change?"
Practice
assert statement do in Python?Solution
Step 1: Understand the purpose of assert
The assert statement tests a condition and raises an error if the condition is false.Step 2: Compare options with assert behavior
Only Checks if a condition is true and stops the program if false correctly describes assert's behavior of stopping the program when the condition is false.Final Answer:
Checks if a condition is true and stops the program if false -> Option DQuick Check:
Assert checks condition and stops if false [OK]
- Thinking assert runs loops
- Confusing assert with print
- Believing assert defines functions
Solution
Step 1: Recall assert syntax
The correct syntax isassert condition, messagewith a comma separating condition and message.Step 2: Check each option
Only assert x > 0, "x must be positive" uses a comma correctly between condition and message.Final Answer:
assert x > 0, "x must be positive" -> Option AQuick Check:
Assert syntax uses comma before message [OK]
- Using colon or semicolon instead of comma
- Missing comma before message
- Putting message without quotes
def check_age(age):
assert age >= 18, "Age must be at least 18"
return "Access granted"
print(check_age(20))
print(check_age(16))Solution
Step 1: Analyze first function call
check_age(20) passes the assert since 20 >= 18, so it returns "Access granted" and prints it.Step 2: Analyze second function call
check_age(16) fails the assert because 16 < 18, so it raises AssertionError with the message.Final Answer:
Access granted\nAssertionError: Age must be at least 18 -> Option BQuick Check:
Assert stops program on false condition with error [OK]
- Thinking both print statements run
- Ignoring the error message
- Assuming assert prints message without error
assert x > 10 "x should be greater than 10"
Solution
Step 1: Check assert syntax
Assert requires a comma between the condition and the message string.Step 2: Identify the error
The code misses the comma, causing a syntax error.Final Answer:
Missing comma between condition and message -> Option AQuick Check:
Comma separates condition and message in assert [OK]
- Using colon or space instead of comma
- Thinking parentheses are mandatory
- Believing assert can't have messages
nums = [3, 5, -1, 7]
for n in nums:
?Solution
Step 1: Understand assert in loop context
We want to check each number and stop if any is not positive, showing which one failed.Step 2: Check syntax correctness
assert n > 0, f"Number {n} is not positive" uses correct assert syntax with a comma and f-string for message. Others use invalid punctuation.Final Answer:
assert n > 0, f"Number {n} is not positive" -> Option CQuick Check:
Assert syntax: condition, message with comma [OK]
- Using colon or semicolon instead of comma
- Not using f-string for variable message
- Putting message outside assert statement
