Comparison operators in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how fast comparison operators run when used in code.
Specifically, we ask: how does the time to compare values change as inputs grow?
Analyze the time complexity of the following code snippet.
values = [5, 3, 8, 6]
threshold = 4
count = 0
for v in values:
if v > threshold:
count += 1
print(count)
This code counts how many numbers in a list are greater than a threshold.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparison using > operator inside a loop.
- How many times: Once for each item in the list.
Each new item adds one more comparison operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets bigger.
[X] Wrong: "Comparison operators take constant time no matter what, so time complexity is always O(1)."
[OK] Correct: While one comparison is quick, doing many comparisons in a loop adds up, so total time depends on how many comparisons happen.
Understanding how comparisons add up helps you explain how your code handles bigger inputs clearly and confidently.
"What if we replaced the list with a nested list and compared items inside inner lists? How would the time complexity change?"
Practice
Solution
Step 1: Understand the equality operator
The operator==is used to check if two values are equal in Python.Step 2: Differentiate from assignment and other operators
=is for assignment,!=means not equal, and>means greater than.Final Answer:
== -> Option AQuick Check:
Equality check = == [OK]
- Confusing = with ==
- Using != for equality
- Using > instead of ==
x is not equal to 10 in Python?Solution
Step 1: Identify the correct not equal operator
In Python,!=is the correct operator for 'not equal'.Step 2: Recognize invalid syntax
<>is not valid in Python,=!and!==are syntax errors.Final Answer:
x != 10 -> Option AQuick Check:
Not equal operator = != [OK]
- Using <> which is invalid in Python
- Writing =! instead of !=
- Using !== from other languages
print(5 > 3 and 2 == 2)
Solution
Step 1: Evaluate each comparison
5 > 3 is True, and 2 == 2 is True.Step 2: Apply the 'and' operator
True and True results in True.Final Answer:
True -> Option CQuick Check:
Both conditions True, so output True [OK]
- Confusing 'and' with 'or'
- Misreading comparison results
- Expecting syntax error
if 4 =< 5:
print("Yes")Solution
Step 1: Check the comparison operator
The operator=<is not valid in Python; the correct operator is<=.Step 2: Verify other syntax parts
The colon is present and indentation is correct; 4 is a valid integer literal for comparison.Final Answer:
The operator =< is invalid in Python -> Option BQuick Check:
Use <= for less than or equal [OK]
- Swapping operator symbols
- Missing colon
- Incorrect indentation
n is between 10 and 20 inclusive. Which expression correctly uses comparison operators in Python?Solution
Step 1: Understand inclusive range check
Inclusive means n can be equal to 10 or 20, so use <= operators.Step 2: Evaluate each option
10 <= n <= 20 uses chained comparison which is correct and concise. 10 < n < 20 excludes 10 and 20. n >= 10 and n < 20 excludes 20. n > 10 or n < 20 uses 'or' which is incorrect for range check.Final Answer:
10 <= n <= 20 -> Option DQuick Check:
Use chained <= for inclusive range [OK]
- Using < instead of <=
- Using 'or' instead of 'and'
- Not including boundary values
