NelsonLabs
Python Basics/Conditionals

Conditionals

Python conditionals use significant whitespace (indentation) instead of curly braces to define code blocks. This forced indentation is controversial but produces consistently readable code.

if / elif / else
python
score = 78

if score >= 90:
    print("A โ€” Excellent")
elif score >= 75:
    print("B โ€” Good")
elif score >= 60:
    print("C โ€” Pass")
else:
    print("F โ€” Try again")

# Inline if (ternary equivalent)
grade = "pass" if score >= 60 else "fail"

# Multiple conditions
age      = 25
has_id   = True
is_member = False

if age >= 18 and has_id:
    print("Entry allowed")

if is_member or age < 12:
    print("Discounted price")

# Check for None
result = get_data()
if result is None:
    print("No data returned")
if result is not None:
    process(result)

WARNING

Indentation is syntax โ€” not style. Python uses indentation to define code blocks instead of curly braces. Consistent indentation (4 spaces is the standard) is not optional โ€” inconsistent indentation causes IndentationError. Configure your editor to show whitespace and use 4-space indentation.