🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #28

Ternary Operator


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 CodingNow 2.0'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 CodingNow 2.0 explains Ternary Operator in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, 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 CodingNow 2.0'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 CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now