Logical operators in conditions in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Logical operators in conditions help decide which code runs based on true or false checks.
We want to see how these checks affect the time it takes for the program to run as input changes.
Analyze the time complexity of the following code snippet.
def check_values(nums, threshold):
count = 0
for num in nums:
if num > threshold and num % 2 == 0:
count += 1
return count
This code counts how many numbers in a list are greater than a threshold and even.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the list.
- How many times: Once for every number in the input list.
As the list gets bigger, the program checks each number once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows directly with the list size.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets bigger.
[X] Wrong: "Logical operators make the code run slower by multiplying the time."
[OK] Correct: The checks happen together for each item, so they add a small fixed cost, not extra loops.
Understanding how conditions affect time helps you explain your code clearly and shows you know what happens as data grows.
"What if we changed the condition to use 'or' instead of 'and'? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of 'and'
Theandoperator returns true only if both conditions are true.Step 2: Compare with other operators
orneeds only one true condition,notreverses truth, andxoris not a Python keyword.Final Answer:
and -> Option BQuick Check:
All conditions true = and [OK]
- Confusing 'and' with 'or'
- Thinking 'not' means all true
- Using 'xor' which is not a Python keyword
x is NOT equal to 10 and variable y is greater than 5?Solution
Step 1: Check the inequality operator
!=is the correct 'not equal' operator in Python;<>is invalid syntax.Step 2: Check logical operator usage
andcorrectly combines two conditions;&is a bitwise operator andorchanges logic.Final Answer:
if x != 10 and y > 5: -> Option AQuick Check:
Correct inequality and 'and' syntax [OK]
- Using '<>' instead of '!='
- Using '&' instead of 'and'
- Using 'or' when 'and' is needed
age = 20
has_id = False
if age >= 18 and has_id:
print('Allowed')
else:
print('Denied')Solution
Step 1: Evaluate each condition
age >= 18is True since 20 >= 18, buthas_idis False.Step 2: Apply 'and' operator
True and False is False, so theelseblock runs.Final Answer:
Denied -> Option CQuick Check:
True and False = False [OK]
- Assuming one True is enough with 'and'
- Confusing 'and' with 'or'
- Ignoring boolean value of variables
if not x > 10 or y < 5
print('Check passed')Solution
Step 1: Check syntax of if statement
Python requires a colon ':' at the end of the if condition line.Step 2: Review other parts
The use ofnotandoris correct; indentation is not shown as wrong here.Final Answer:
Missing colon ':' after condition -> Option AQuick Check:
if statements need ':' [OK]
- Forgetting ':' after if
- Misusing 'not' operator
- Wrong indentation without colon
n is either less than 0 or greater than 100, but NOT equal to -10. Which condition correctly uses logical operators?Solution
Step 1: Understand the condition requirements
We want numbers less than 0 or greater than 100, but exclude -10.Step 2: Analyze each option
if (n < 0 or n > 100) and n != -10: correctly groups the or condition and excludes -10 withand n != -10. if n < 0 or (n > 100 and n != -10): excludes -10 only when >100, not <0 (e.g., n=-10 is true). if not (n < 0 or n > 100) and n == -10: reverses logic and checks for equal -10. if n < 0 and n > 100 or n != -10: mixes and/or incorrectly.Final Answer:
if (n < 0 or n > 100) and n != -10: -> Option DQuick Check:
Group or, then exclude -10 with and [OK]
- Wrong grouping of conditions
- Misplacing 'not' or 'and'
- Confusing '!=' with '=='
