Back to Python Notes
Topic #28

Ternary Operator

Python's ternary lets you assign one of two values based on a condition in a single line. It is the compact way to express simple either-or logic.


The Ternary Operator

The ternary operator allows you to assign one value if a condition is true, and another if it is false:

Example

num = 6

x = "WEEKEND!" if num > 5 else "Workday"

print(x)

Note: The ternary operator is not an actual operator, it is a conditional expression, or a shorthand if statement.


Instead of Elif:

The ternary operator can be used instead of elif in longer if statements:

Example

num = 6

x = "Fri" if num == 5 else "Sat" if num == 6 else "Sun" if num == 7 else "weekday"

print(x)

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Ternary Operator – FAQs

Quick answers about learning Ternary Operator in Python.

This free note from Coding Now Tech Institute explains Ternary Operator in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Ternary Operator, is 100% free with no signup required.
With focused practice, most students grasp Ternary Operator in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now