Challenge - 5 Problems
Ternary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested ternary conditional expression
What is the output of this Python code?
Python
x = 5 y = 10 result = 'A' if x > y else 'B' if x == y else 'C' print(result)
Attempts:
2 left
💡 Hint
Remember how chained ternary expressions evaluate from left to right.
✗ Incorrect
The expression checks if x > y (5 > 10) which is False, then checks if x == y (5 == 10) which is also False, so it returns 'C'.
❓ Predict Output
intermediate2:00remaining
Ternary expression with function calls
What will be printed by this code?
Python
def f(): return 1 def g(): return 2 value = f() if False else g() print(value)
Attempts:
2 left
💡 Hint
The condition is False, so the else part runs.
✗ Incorrect
Since the condition is False, the else part calls g() which returns 2.
❓ Predict Output
advanced2:00remaining
Ternary expression with side effects
What is the output of this code snippet?
Python
x = 0 result = (x := x + 1) if True else (x := x + 2) print(x, result)
Attempts:
2 left
💡 Hint
The walrus operator assigns and returns the value.
✗ Incorrect
The condition is True, so (x := x + 1) runs, x becomes 1 and result is 1.
❓ Predict Output
advanced2:00remaining
Ternary expression with different data types
What will this code print?
Python
value = 10 result = 'Even' if value % 2 == 0 else 0 print(type(result))
Attempts:
2 left
💡 Hint
The ternary expression returns either a string or an integer.
✗ Incorrect
Since 10 is even, the expression returns 'Even' which is a string, so type(result) is .
🧠 Conceptual
expert2:00remaining
Understanding ternary expression evaluation order
Consider the expression:
What is the value of
result = 'X' if False else 'Y' if True else 'Z'
What is the value of
result after this runs?Attempts:
2 left
💡 Hint
Ternary expressions are evaluated left to right, and else can contain another ternary.
✗ Incorrect
The first condition is False, so it evaluates the else part which is another ternary: 'Y' if True else 'Z'. Since True is True, it returns 'Y'.