Elvis operator
The Elvis operator is a binary operator, written ?:, that returns its left operand when that operand is truthy and otherwise evaluates and returns its right operand. It’s a shorthand for the ternary conditional a ? a : b, a form of syntactic sugar that also evaluates the left operand only once, avoiding a repeated side effect.
The name comes from a visual pun. Rotated ninety degrees, the ?: symbols look like an emoticon of the musician Elvis Presley, with the colon as his eyes and the question mark’s hook as his pompadour hairstyle. What triggers the fallback to the right operand depends on the language:
- Truthy-based: Operators in Groovy and PHP fall through whenever the left operand is falsy, so an empty string, a zero, or an empty collection all yield the fallback.
- Null-coalescing: Operators in Kotlin return the left operand unless it’s exactly
null, mirroring the separate??operator in languages such as JavaScript and C#.
The figure below feeds the operator different left operands under each behavior, showing which operand it returns and whether the right one is evaluated at all.
Python has no Elvis operator. Its closest idiom is the short-circuiting or operator, where a or b returns a when that operand is truthy and b otherwise.
Related Resources
Tutorial
Operators and Expressions in Python
In Python, operators are special symbols, combinations of symbols, or keywords that designate some type of computation. You can combine objects and operators to build expressions that perform the actual computation. So, operators are the building blocks of expressions.
For additional information on related topics, take a look at the following resources:
- Using the "or" Boolean Operator in Python (Tutorial)
- Using the "and" Boolean Operator in Python (Tutorial)
- Using the "not" Boolean Operator in Python (Tutorial)
- Python's Assignment Operator: Write Robust Assignments (Tutorial)
- Python Operators and Expressions (Course)
- Operators and Expressions in Python (Quiz)
- Using the Python or Operator (Course)
- Using the "or" Boolean Operator in Python (Quiz)
- Using the Python and Operator (Course)
- Using the Python not Operator (Course)
- Python's Assignment Operator: Write Robust Assignments (Quiz)
By Martin Breuss • Updated Aug. 7, 2026