Lesson 8 / 47

If ... Else

Branching with elif and truthiness.

if / elif / else

Note the colon and the indented block. Python uses elif.

if temp > 25:
    print("Warm")
elif temp > 10:
    print("Mild")
else:
    print("Cold")

Truthiness

Falsy: False, None, 0, "", [], {}, set(). Everything else is truthy — so if items: checks non-emptiness.

Conditional expression

label = "even" if n % 2 == 0 else "odd"