Boolean values (True and False) in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with Boolean values like True and False, it's helpful to see how quickly operations run as input changes.
We want to know how the time to check or use these values grows when we have more data.
Analyze the time complexity of the following code snippet.
values = [True, False, True, False, True]
count_true = 0
for val in values:
if val is True:
count_true += 1
print(count_true)
This code counts how many True values are in a list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the list.
- How many times: Once for every item in the list.
As the list gets bigger, the time to count True values grows in a simple way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of steps grows directly with the number of items.
Time Complexity: O(n)
This means the time to count True values grows in a straight line as the list gets longer.
[X] Wrong: "Checking for True values takes the same time no matter how many items there are."
[OK] Correct: Each item must be checked one by one, so more items mean more time.
Understanding how simple checks grow with input size helps you explain your code clearly and think about efficiency in real tasks.
"What if we stopped counting as soon as we found the first True? How would the time complexity change?"
Practice
Solution
Step 1: Understand Boolean values
Boolean values in Python are exactlyTrueorFalse, not strings or numbers.Step 2: Identify the correct Boolean
True is the Boolean valueTrue. "yes", "True", and 1 are not Boolean types.Final Answer:
True -> Option CQuick Check:
Boolean = True or False [OK]
- Confusing string "True" with Boolean True
- Using numbers like 1 as Boolean
- Thinking any word means Boolean
Solution
Step 1: Recall Python Boolean syntax
Python Boolean values start with a capital letter:TrueandFalse.Step 2: Check each option
Only False,False, is correctly capitalized. Others are lowercase or all caps, which are invalid.Final Answer:
False -> Option AQuick Check:
Booleans start with uppercase [OK]
- Writing booleans in lowercase
- Using all uppercase letters
- Confusing with strings
print(5 > 3)
Solution
Step 1: Evaluate the comparison
The expression5 > 3checks if 5 is greater than 3, which is true.Step 2: Understand print output
Printing a Boolean expression printsTrueorFalseaccordingly. Here it printsTrue.Final Answer:
True -> Option DQuick Check:
5 > 3 is True [OK]
- Thinking it prints the expression as text
- Confusing True with string 'True'
- Expecting an error from comparison
if True = 5:
print("Yes")Solution
Step 1: Check the if statement syntax
The code uses=which is assignment, not comparison. Conditions need==.Step 2: Identify the error type
Using=in an if condition causes a syntax error because assignment is not allowed there.Final Answer:
Assignment used instead of comparison -> Option AQuick Check:
Use == for comparison, not = [OK]
- Using = instead of ==
- Forgetting colon after if
- Thinking True can't be in conditions
n is NOT zero using Boolean logic. Which code correctly prints True if n is not zero, otherwise False?Solution
Step 1: Understand the condition
We want to printTrueifnis NOT zero, so the condition should checkn != 0.Step 2: Evaluate each option
print(n == 0) printsTrueifnis zero (wrong). print(n != 0) correctly printsTrueifnis not zero. print(n = 0) causes a syntax error (invalid syntax). print(not n) printsTrueifnis zero (becausenot 0isTrue), so it's opposite.Final Answer:
print(n != 0) -> Option BQuick Check:
Use != to check not equal [OK]
- Using = instead of !=
- Confusing not n with n != 0
- Using == instead of !=
