Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 03 / Topic 3 of 12

Conditional expression (one-line if)

value_if_true if condition else value_if_false gives back a value. Think of it as a quick "this or that" choice. Use it for short, simple choices only. Do not nest them; use a normal if instead.

Real-world example: show "Free delivery" for orders of ₹499 or more.

order_total = 650
delivery = "Free delivery" if order_total >= 499 else "₹40 delivery fee"
print(delivery)  # Free delivery

stock = 0
print("In stock" if stock > 0 else "Out of stock")  # Out of stock

Done here? The practice set for Part 3 — 7 exercises with solutions — is on the part page.

Search the guide