| jupytext |
|
||||||
|---|---|---|---|---|---|---|---|
| kernelspec |
|
Learn conditional expressions in 5 minutes! ❓ (5:21)
Write compact, elegant one-line conditional statements using Python's ternary operator!
- Understand the ternary operator syntax
- Convert if-else statements to one-liners
- Know when to use ternary vs traditional if-else
- Write cleaner, more pythonic code
A ternary operator (conditional expression) is a compact way to write simple if-else statements in one line.
value_if_true if condition else value_if_false
# Traditional if-else
if age >= 18:
status = "Adult"
else:
status = "Minor"
# Ternary operator (one line)
status = "Adult" if age >= 18 else "Minor"
number = 7
result = "EVEN" if number % 2 == 0 else "ODD"
print(result) # Output: ODD
a, b = 15, 23
maximum = a if a > b else b
print(f"Max: {maximum}") # Output: Max: 23
score = 75
result = "PASS" if score >= 60 else "FAIL"
print(result) # Output: PASS
user_role = "admin"
access = "Full Access" if user_role == "admin" else "Limited Access"
print(access) # Output: Full Access
- Check if number is positive, negative, or zero
- Determine if year is leap year (divisible by 4)
- Assign discount based on purchase amount (>100 = 10%, else 0%)
- Check if password is strong (length >= 8)
- Determine shipping: "Free" if total > 50 else "$5.99"
- Set temperature message: "Cold" if temp < 60 else "Warm"
# Simple value assignment
status = "Open" if is_open else "Closed"
# Function arguments
print("Hello" if is_greeting else "Goodbye")
# List/dict values
data = {"status": "active" if user.is_active else "inactive"}
# Return statements
return True if value > 0 else False
# Complex logic (use regular if-else)
result = "A" if score >= 90 else "B" if score >= 80 else "C" if score >= 70 else "F"
# Too hard to read!
# Multiple statements
message = (print("Hello"), user.save(), "Done") if condition else "Skip"
# Not allowed - ternary is for expressions, not statements
# Can be hard to read
grade = "A" if score >= 90 else "B" if score >= 80 else "C" if score >= 70 else "F"
# Better: use regular if-elif-else for multiple conditions
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
total = 120
discount = 0.10 if total >= 100 else 0.05 if total >= 50 else 0
savings = total * discount
final = total - savings
print(f"Discount: {discount*100}%, You save: ${savings:.2f}")
points = 1500
badge = "🥇 Gold" if points >= 1000 else "🥈 Silver" if points >= 500 else "🥉 Bronze"
print(f"Your badge: {badge}")
- Create age category checker (Child/Teen/Adult/Senior)
- Build temperature converter with unit check
- Make a simple calculator using ternary for operation selection
- Create BMI category determiner
- Build a simple voting eligibility checker
- Variables store data values that can be reused
- Use if-elif-else for conditional logic
- Follow along with the video for hands-on practice
💡 These points cover the main concepts from the video tutorial to help reinforce your learning.
Continue to Chapter 13: String Methods to explore string manipulation techniques!