Conditional Statements
if / elif / else — how Python makes decisions.
subscribers = 250_000
if subscribers >= 1_000_000:
print("Diamond Play Button")
elif subscribers >= 100_000:
print("Silver Play Button")
elif subscribers >= 1_000:
print("Just getting started")
else:
print("Keep uploading!")
Python checks each condition top to bottom and runs the first block whose condition is True — the rest are skipped, even if they'd also be true. elif is short for "else if"; else is optional and catches everything not matched above.
Nesting conditions
You can nest an if inside another, but past 2–3 levels it usually reads better refactored with elif or early returns inside a function.
channel = "CodeWithMunnaX"
is_verified = True
if channel == "CodeWithMunnaX":
if is_verified:
print("Verified CodeWithMunnaX channel")
else:
print("Unverified account with that name")
The ternary (conditional) expression
A compact one-line if/else that produces a value, not a statement — useful for a quick inline choice.
subscribers = 250_000
status = "Popular" if subscribers > 100_000 else "Growing"
print(status) # Popular
Truthy checks without == True
Never write if is_verified == True: — just write if is_verified:. Any value can be used directly as a condition; Python checks its truthiness (see the Type Conversion topic for what counts as falsy).
comments = []
if comments: # False for an empty list
print(comments[0])
else:
print("No comments yet")- •Python runs the first matching if/elif branch and skips the rest, even if a later condition would also be true.
- •else is optional and catches whatever no earlier condition matched.
- •value_if_true if condition else value_if_false is a ternary expression — it evaluates to a value, unlike a full if statement.
- •Write if is_verified:, never if is_verified == True: — let Python's own truthiness do the check.
Play Button tiers with if/elif/else
Revision Flashcards
Tap a card to flip it and see the answer.