If / Elif / Else — Conditional Logic
Write conditional logic to classify, flag, and filter data in Python
Conditional statements let your code make decisions based on data values. In data analysis, you use conditionals constantly — to classify customers into tiers, flag outliers, assign labels, and apply business rules.
Python uses if / elif / else. Indentation (4 spaces) defines the code block — there are no curly braces like in other languages.
Syntax
if condition:
# runs if condition is True
elif another_condition:
# runs if first is False but this is True
else:
# runs if all conditions are FalseExamples
Key Points
- ✓Python uses indentation (4 spaces) for code blocks — not curly braces
- ✓elif = "else if" — check multiple conditions in order
- ✓Comparison operators: == != > < >= <=
- ✓Logical operators: and, or, not (Python uses words, not && ||)
- ✓In pandas: np.where > np.select > apply() — in order of performance
Practice Question
Which operator checks if two values are equal in Python?