devnotes.

if/elif/else and Conditional Expressions

Python uses indentation rather than braces to define code blocks, and spells the "else if" chain as a single word, elif.

hour = 14
if hour < 12:
    print("Morning")
elif hour < 18:
    print("Afternoon")
else:
    print("Evening")

label = "AM" if hour < 12 else "PM"   # conditional (ternary) expression

# Any of these values are considered "falsy" in a condition:
# False, None, 0, 0.0, "", [], {}, set() — everything else is "truthy"
items = []
if not items:
    print("List is empty")

Indentation is syntactically meaningful in Python — it's not just a style choice, the interpreter uses it to determine where a block starts and ends, so inconsistent indentation (mixing tabs and spaces, or misaligned blocks) causes an actual IndentationError. The standard convention is 4 spaces per indent level, and virtually every editor and style guide enforces this.

Python's conditional expression, value_if_true if condition else value_if_false, reads left-to-right differently than C's condition ? a : b — take care not to confuse the order when switching between languages.

Because Python has clear rules for what counts as falsy (empty collections, 0, None, False, empty strings) versus truthy (everything else), if not items: is the idiomatic way to check "is this list/string/dict empty?" rather than the more verbose if len(items) == 0:.

if/elif/else and Conditional Expressions in Python | DevNotes