age = 20
if age >= 18:
print('You are an adult')
# One-liner (only for simple cases)
if age >= 18: print('Adult')age = 15
if age >= 18:
print('Adult')
else:
print('Minor')
# One-liner (ternary / conditional expression)
status = 'Adult' if age >= 18 else 'Minor'
print(status) # 'Minor'score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'F'
print(f'Grade: {grade}') # 'B'Key points:
elif is short for 'else if'else block is optionalage = 25
has_license = True
if age >= 18:
if has_license:
print('Can drive')
else:
print('Needs license')
else:
print('Too young to drive')
# Better: flatten with 'and'
if age >= 18 and has_license:
print('Can drive')
elif age >= 18:
print('Needs license')
else:
print('Too young')# These are all VALID conditions
name = 'Alice'
if name: # Truthy (non-empty string)
print(f'Hello {name}')
items = []
if not items: # Falsy (empty list)
print('Cart is empty')
value = 0
if value is not None: # Check for None specifically
print(f'Value: {value}')
# Dangerous: 0, '', [], None, False all evaluate to False
x = 0
if x: # This is False!
print('Won\'t print')
if x is not None: # This correctly distinguishes 0 from None
print(f'Value is {x}') # This prints!# Early return pattern (guard clauses)
def process(data):
if data is None:
return None
if len(data) == 0:
return []
# Main logic here
return [x * 2 for x in data]
# Match-case (Python 3.10+)
command = 'start'
match command:
case 'start':
print('Starting...')
case 'stop':
print('Stopping...')
case 'pause' | 'resume':
print('Toggling...')
case _:
print('Unknown command')Create a function `calculate_bmi(weight_kg, height_m)` that calculates BMI and returns a tuple: (bmi_value, category). Categories: Underweight (<18.5), Normal (18.5-24.9), Overweight (25-29.9), Obese (≥30). Format BMI to 1 decimal place. Also create a function `get_discount(price, customer_type)` where customer_type is 'student', 'senior', 'regular' — student gets 20%, senior 15%, regular 0%. Return discounted price.