Skip to content

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.

Interactive diagram — enable JavaScript to view.

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.

Operators and Expressions in Python

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.

basics python

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated Aug. 7, 2026