BeginnerCoder

Lesson 14 — Ternary expressions

One-line choices

A conditional expression (often called a ternary) picks one of two values in a single line. The pattern reads almost like English: value_if_true if condition else value_if_false. Python checks the condition first, then returns only one of the two values.

temp = 30
weather = "hot" if temp > 25 else "mild"

Why it matters: it keeps simple either/or choices short and readable, without a four-line if/else block.

Want it another way?
Task

Set label to "even" or "odd" for n using a ternary, then print it.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.