Learning

Conditional Statements

Conditional Statements

if Statement

python
age = 20 if age >= 18: print('You are an adult') # One-liner (only for simple cases) if age >= 18: print('Adult')

if / else

python
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'

if / elif / else

python
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:

  • Only ONE block executes (first match wins)
  • elif is short for 'else if'
  • The else block is optional
  • Order matters — put most specific conditions first!

Nested Conditions

python
age = 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')

Truthiness in Conditions

python
# 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!

Common Patterns

python
# 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')
Key Rules
  • •elif checks are mutually exclusive — only the FIRST matching block runs
  • •Order matters: put most specific/restrictive conditions BEFORE general ones
  • •Use 'and' to flatten nested ifs when possible — improves readability
  • •Use 'is None' / 'is not None' instead of == None or not x (avoids 0/''/[] confusion)
  • •Ternary expression: value_if_true IF condition ELSE value_if_false — note the order!
  • •Match-case (Python 3.10+) is cleaner than long if/elif chains for single-value checks
Your Task

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.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should define calculate_bmi function
Should use BMI formula weight / height^2
Should use elif for BMI categories
Should define get_discount function
Should use dict or if/elif for discount mapping
Should test with specific values