Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Python Conditional Expressions

Learn how to choose between two values with Python’s conditional expression, while keeping the expression readable and distinct from a full if statement.

Levels: Beginner, Intermediate ~12 min read

Basic

Python can choose a value without choosing a whole block of statements. A conditional expression puts the two possible values and the condition into one expression, so the result can be used wherever an expression is expected.

The basic shape

python basic.py
1
2
3
4
5
age = 20
label = "adult" if age >= 18 else "minor"
print(label)

# The expression chooses one value; it does not run two branches.

Read it as: choose "adult" if the condition is true; otherwise choose "minor".

The syntax is `value_if_true if condition else value_if_false`. Python evaluates the condition first. If it is true, the first value is evaluated and returned; otherwise the second value is evaluated and returned.

It produces a value

python value.py
1
2
3
4
5
temperature = 17
message = "warm" if temperature >= 20 else "cool"

print(message)
print("warm" if temperature >= 20 else "cool")

Because a conditional expression produces a value, you can assign it or pass it directly to another function.

This is the important difference from the `if` statement you already know: an `if` statement controls which block of statements runs, while a conditional expression computes one value.

Choose between computed values

python computed.py
1
2
3
4
5
6
7
8
score = 83
result = "pass" if score >= 50 else "try again"

price = 100
discounted = price * 0.9 if score >= 90 else price

print(result)
print(discounted)

The two alternatives can themselves be expressions, not just literal strings.

Only the selected value expression is evaluated. That makes a conditional expression useful when choosing between computations, not merely between fixed labels.

Use it inside larger expressions

python composition.py
1
2
3
4
5
6
7
8
items = 3
unit_price = 12

subtotal = items * unit_price
shipping = 0 if subtotal >= 30 else 5
total = subtotal + shipping

print(total)

A conditional expression can be one part of a larger expression.

Because the result is an ordinary value, it can participate in arithmetic, function calls, assignments, and other expressions. Keep the surrounding expression easy to read; compact syntax is not automatically better syntax.

Keep the condition easy to read

python readability.py
1
2
3
4
5
logged_in = True
is_admin = False

access = "admin" if logged_in and is_admin else "standard"
print(access)

A short, clear condition can fit naturally inside the expression.

If the condition becomes difficult to understand, calculate a named boolean first or use a regular `if` statement. The goal is readable Python, not the shortest possible line.

Nested conditional expressions need care

python nested.py
1
2
3
4
5
6
score = 72
label = ("excellent" if score >= 90 else
         "pass" if score >= 50 else
         "retry")

print(label)

Nested conditional expressions are legal, but parentheses and formatting help readers see the choices.

Conditional expression vs if statement

python choose.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
age = 21

# Value selection: conditional expression
label = "adult" if age >= 18 else "minor"

# Statement control: if statement
if age >= 18:
    print("You may enter.")
else:
    print("You may not enter.")

Use the expression when you need a value; use the statement when you need to control a block of work.

A good rule is simple: if the question is “which value should I use?”, a conditional expression may fit. If the question is “which work should the program perform?”, an `if` statement is usually clearer.

Was this page helpful?

Mark this learning resource complete to track your learning progress.