Challenge - 5 Problems
Logical Operators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combined logical operators
What is the output of this code snippet?
Python
a = True b = False c = True result = a and b or c print(result)
Attempts:
2 left
💡 Hint
Remember that 'and' has higher precedence than 'or'.
✗ Incorrect
The expression evaluates as (a and b) or c. Since a and b is False, the result is c which is True.
❓ Predict Output
intermediate2:00remaining
Result of chained logical expressions
What will be printed by this code?
Python
x = 5 print((x > 3) and (x < 10) or (x == 5))
Attempts:
2 left
💡 Hint
Check each comparison and how 'and' and 'or' combine them.
✗ Incorrect
x > 3 is True, x < 10 is True, so (True and True) is True. True or (x == 5) is True.
❓ Predict Output
advanced2:00remaining
Output of logical operators with non-boolean values
What is the output of this code?
Python
result = '' or 0 and 'Hello' or None print(result)
Attempts:
2 left
💡 Hint
Remember that 'and' has higher precedence than 'or' and that logical operators return the last evaluated operand.
✗ Incorrect
The expression evaluates as: '' or (0 and 'Hello') or None. 0 and 'Hello' evaluates to 0 because 'and' returns the first falsy value or last value. Then '' or 0 evaluates to 0 (since '' is falsy). Finally, 0 or None evaluates to None if 0 is falsy, but since 0 is falsy, the last 'or None' returns None. However, in Python, 'or' returns the first truthy value or the last value if none are truthy. Here, '' is falsy, 0 is falsy, so '' or 0 returns 0, then 0 or None returns None. But since 0 is falsy, the final result is None. Actually, the correct evaluation is: '' or (0 and 'Hello') or None -> '' or 0 or None -> 0 or None -> None. So the output is None.
❓ Predict Output
advanced2:00remaining
Output of complex logical expression with parentheses
What does this code print?
Python
a = False b = True c = False print((a or b) and (b or c))
Attempts:
2 left
💡 Hint
Evaluate inside parentheses first, then combine with 'and'.
✗ Incorrect
(a or b) is True, (b or c) is True, so True and True is True.
❓ Predict Output
expert2:00remaining
Value of variable after logical operations with assignment
What is the value of variable 'x' after running this code?
Python
x = None x = x or [] x.append(1) x = x and [2, 3] x.append(4) print(x)
Attempts:
2 left
💡 Hint
Track how 'x' changes after each line and how logical operators affect assignment.
✗ Incorrect
Initially x is None, then x = x or [] sets x to []. Then x.append(1) makes x = [1]. Then x = x and [2, 3] sets x to [2, 3] because x is truthy. Then x.append(4) adds 4 to the list, so x = [2, 3, 4].