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 Python code snippet?
Python
x = 5 if x > 3 and not x == 5: print("A") elif x > 3 or x == 5: print("B") else: print("C")
Attempts:
2 left
💡 Hint
Remember how 'and', 'or', and 'not' work together in conditions.
✗ Incorrect
The first condition checks if x > 3 and x is not 5. Since x is 5, 'not x == 5' is False, so the first if fails. The elif checks if x > 3 or x == 5, which is True, so it prints 'B'.
❓ Predict Output
intermediate2:00remaining
Evaluating multiple logical conditions
What will be printed by this code?
Python
a = True b = False c = True if a and b or c: print("Yes") else: print("No")
Attempts:
2 left
💡 Hint
Remember the precedence of 'and' over 'or'.
✗ Incorrect
The expression 'a and b or c' is evaluated as '(a and b) or c'. Since a and b is False, but c is True, the overall condition is True, so it prints 'Yes'.
❓ Predict Output
advanced2:00remaining
Output with mixed logical operators and parentheses
What is the output of this code?
Python
x = 10 if (x < 5 or x > 8) and not (x == 10): print("First") elif x == 10 and not (x < 5): print("Second") else: print("Third")
Attempts:
2 left
💡 Hint
Check each condition carefully with parentheses.
✗ Incorrect
The first if condition is False because although (x < 5 or x > 8) is True, 'not (x == 10)' is False. The elif condition is True because x == 10 and not (x < 5) is True, so it prints 'Second'.
❓ Predict Output
advanced2:00remaining
Logical operators with short-circuit behavior
What will this code print?
Python
def check(): print("Checked") return False if False and check(): print("Yes") else: print("No")
Attempts:
2 left
💡 Hint
Remember how 'and' stops evaluating if the first part is False.
✗ Incorrect
Because the first part of 'and' is False, Python does not call check(), so 'Checked' is not printed. The else block runs and prints 'No'.
❓ Predict Output
expert2:00remaining
Complex logical condition output
What is the output of this code?
Python
x = 3 y = 4 z = 5 if (x > y or y < z) and not (x == 3 and z == 5): print("Alpha") elif x == 3 and (y == 4 or z == 6): print("Beta") else: print("Gamma")
Attempts:
2 left
💡 Hint
Evaluate each part step by step, especially the 'not' condition.
✗ Incorrect
The first if condition is False because although (x > y or y < z) is True, the 'not (x == 3 and z == 5)' is False (since x==3 and z==5 is True). The elif condition is True because x==3 and (y==4 or z==6) is True, so it prints 'Beta'.