Nested Conditions and Ternary Expressions
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)When a secondary decision depends on an initial condition succeeding, we can nest conditionals. For simple two-way assignments, Python provides a compact ternary expression.
Nested if statements
Nested if is an if statement placed inside the block of another if. The inner check runs only when the outer condition is True, so the inner block sits one level deeper.
An if statement placed inside another if block executes only when the outer condition is true.
account_active = Truebalance = 250
if account_active: if balance >= 100: print("Transaction approved") else: print("Low balance")else: print("Account suspended")Example 1 · Loan pre-check
age = 25income = 40000
if age >= 21: if income >= 30000: print("Application accepted") else: print("Income too low")else: print("Applicant too young")Application accepted
What this code does
- The income check only runs when the age check passes.
- Both are True here, so the innermost print runs. The two else lines belong to their own if.
Example 2 · Same check, one level
is_logged_in = Truecart_items = 2
if is_logged_in and cart_items > 0: print("Go to payment")else: print("Cannot check out")Go to payment
What this code does
- Two nested ifs with no code between them can become one if joined by
and. - The flat version is shorter and has one less level of indentation.
Each nested level adds 4 more spaces of indentation. Keep nesting shallow (at most 2-3 levels) for maintainability.
Python's ternary operator: inline if-else
Ternary expression is a one-line expression of the form a if condition else b. It gives a when the condition is True and b when it is False, so it can be assigned or printed directly.
For concise conditional assignment, Python uses the syntax: value_if_true if condition else value_if_false.
age = 20status = "Adult" if age >= 18 else "Minor"print(status)Example 3 · Stock label
stock = 0label = "In stock" if stock > 0 else "Sold out"print(label)Sold out
What this code does
stock > 0is False, so the ternary gives the text afterelse.- The result is stored in
labellike any other value.
This is Python's equivalent of the C/Java condition ? a : b operator. It reads like natural English prose.
Using ternary expressions in prints and calculations
Because a conditional expression evaluates to a value, you can pass it directly into function calls or calculations.
score = 85print(f"Result: {'Pass' if score >= 50 else 'Fail'}")Example 4 · Member discount
is_member = Truepurchase_amount = 800
discount = 0.15 if is_member and purchase_amount >= 500 else 0.05final_price = purchase_amount * (1 - discount)
print(f"Discount: {int(discount * 100)}%")print(f"Final price: {final_price}")Discount: 15% Final price: 680.0
What this code does
- Uses a ternary expression to select 15% discount for qualifying members.
- Calculates final price and prints formatted summary.
The ternary expression evaluates inline inside the f-string curly braces, printing Result: Pass.
Next step
Check your understanding
8 quick questions