Skip to content

Python Ternary Operator

Syntax

The ternary operator in Python provides a concise way to perform conditional assignments in the format: value_if_true if condition else value_if_false.

# Using a ternary operator
result = value_if_true if condition else value_if_false

Example

age = 18
message = "You are eligible to vote." if age >= 18 else "You are not eligible to vote."
print(message)
# Output: You are eligible to vote.