Python If / Else Foundation
if / elif / else Topic 03 Foundation

Decisions with conditionals

Foundation lesson for decision-making in Python. Read theory first, then explore real-world branching.

Picture this: a fork in the road

if checks a yes/no. Only one branch runs.

Questionis it true?
→
ifyes path
→
elifother yes
→
elseotherwise

Python tests from the top. The first true branch wins.

In easy words: What is a conditional?

A conditional lets your program choose one path based on a condition. If the condition is true, one block runs; otherwise another runs.

Diagram — If / elif / else decision

condition? if True → block A elif → block B else → block C Only one branch runs for a chain of if / elif / else.

Where conditionals are used

Used in login success/failure, payment status, grade assignment, eligibility checks, and almost every app decision.

Rules for beginners

  • Colon required — every if / elif / else ends with :
  • Indentation — the block under each branch is indented (usually 4 spaces).
  • One path — Python picks the first true condition, then skips the rest.
  • Comparisons — >, <, ==, != return True or False.

Real world: umbrella decision

if it is raining → take umbrella. else → leave it home. No umbrella logic runs on sunny days. Code works the same: only the matching branch executes.

Traffic light simulator

🚦
Select a condition
light == "red" ?
STOP — wait
GO — drive
if light == "red": print("STOP") else: print("GO")
Structure map
if condition:
→
elif other:
→
else:

Example Program

1age = 18
2if age >= 18:
3print("Adult")
4else:
5print("Minor")

Conditionals — Topic Quiz

Check understanding

Answer all questions, then click Check answers. Use this before Practice in the editor.

Q1. What does `if` control?

Q2. In if / elif / else, how many branches run?

Q3. What is `n > 0` when n is -3?

Q4. Leap year logic mainly checks divisibility by…

Score: —