Python for Data Analytics • Lesson 12

Conditional Statements

Learn how Python makes decisions using conditions. Work with if, elif and else, and apply them to real-world data analytics situations.

Your progress0%

0 of 8 concepts completed

Complete the activity and related MCQ to unlock the next concept.

Concept 1IF

The if Statement

An if statement runs a block of code only when its condition is True. The condition is followed by a colon, and the body of the if is indented.

Learn by Seeing

Run code only when True

age = 20

if age >= 18:
    print("Adult")

Python checks the condition, then decides whether to run the indented block.

Edit age — see whether the if body would run:

if 20 >= 18:

print("Adult") ← runs

Interactive Activity

What does age >= 18 evaluate to when age = 20?

Hint: 20 is at least 18.

Python Analyst Mindset

Business rules become conditionals

Every "High Value" label, "Bonus Eligible" flag, and "Top Performer" tag is a conditional statement under the hood. Once you can translate a rule into if / elif / else, you can express any business policy in code.

01

Read the rule

What thresholds matter?

02

Order branches

Highest threshold first.

03

Add else

Handle everything below.

04

Test values

Check boundaries (≥ vs >).

Frequently Asked Questions

Conditional Statements — FAQs

Why is indentation required in Python?

Python uses indentation to define the body of if / else blocks. Missing indentation causes an IndentationError.

Can I have multiple elif branches?

Yes. Chain as many elif as you need, and end with an optional else. Python stops at the first matching condition.

What if no branch matches?

Nothing runs unless you included an else. Add an else to handle the fallback case.

Can I nest an if inside an else?

Yes, and it is common. Just remember to keep the indentation consistent so Python knows which block each if belongs to.

Lesson Summary

What you learned

if

Runs when its condition is True.

else

Runs when the preceding condition is False.

elif

Checks another condition after a False one.

Combined conditions

and / or / not combine comparisons inside an if.

Nested if

Inner conditions only run after outer ones pass.

= vs ==

= assigns; == compares.