Hintor
Python Basics for Programmers

Conditions with if, elif, and else

Python for Developers/Python Basics for Programmers/

Conditions with if, elif, and else

Python

Lesson explanation

Conditions with if, elif, and else


🎯 Learning Objective

After this lesson, you will be able to:

  • Choose between if, elif, and else for two-way and multi-way decisions.
  • Combine comparisons with and, or, and not.
  • Order overlapping rules so the most specific valid branch runs first.
  • Produce one clear result from a decision without running unrelated branches.

Short Explanation

A condition controls whether a block of code runs. Use if for the first rule, elif for additional rules, and else for the fallback when no earlier condition matches.

Python checks branches from top to bottom and stops at the first true branch. That makes branch order part of the logic. A broad rule placed too early can hide a more specific one below it.

Conditions can reuse the comparisons and logical operators you already know:

Python
age >= 18
role == "admin" and is_active
is_vip or order_total >= 100
not is_suspended

Each branch line ends with a colon, and the code inside it is indented. This lesson uses that structure; the dedicated blocks lesson will examine indentation, nested blocks, and placeholders more closely.

💡 Tip: When two rules can both be true, place the more specific rule first.


Clean Code Examples

1. Choose between two outcomes

Use if and else when exactly one of two outcomes should be selected.

Python
maintenance_mode = False

if maintenance_mode:
    print("Service unavailable")
else:
    print("Service online")

Output:

TEXT
Service online

Only one branch runs.

2. Classify a value with multiple branches

Use elif when the same value can fall into one of several ordered categories.

Python
battery_level = 42

if battery_level >= 80:
    print("Battery: high")
elif battery_level >= 30:
    print("Battery: medium")
else:
    print("Battery: low")

Output:

TEXT
Battery: medium

The first condition is false, so Python checks the next branch.

3. Combine related checks

Logical operators let one branch depend on several facts.

Python
role = "editor"
is_verified = True

if role == "editor" and is_verified:
    print("Publishing enabled")
else:
    print("Publishing blocked")

Output:

TEXT
Publishing enabled

Both sides of and must be true.

4. Put specific rules before broad rules

The first true branch wins, so rule order matters.

Python
error_count = 12

if error_count >= 10:
    print("Alert: critical")
elif error_count > 0:
    print("Alert: warning")
else:
    print("Alert: clear")

Output:

TEXT
Alert: critical

If the warning rule came first, the critical case would never be reached.



⚠️ Common Mistakes

  1. Do not write else if; Python uses elif.
  2. Do not place a broad condition before a more specific condition that it already includes.
  3. Do not use several independent if statements when only one result should be selected.
  4. Do not forget that else has no condition.
Start Practice

Python for Developers

Conditions with if, elif, and else

6 tasks
Lesson progress0 / 6 tasks completed

Tasks

Practice progress

Complete an API health branch

View task

Complete the missing middle branch so the status code is classified correctly.

Requirements

  • Keep the existing thresholds and messages unchanged.
  • Fill only the missing branch keyword.

Expected output

API: degraded
Code: PythonTask 1

Complete an API health branch

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3
4
5
6
7
8
9

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 6 tasks completed

Previous lessonBack to unitBack to track
DecisionPython structure
Run code only when a rule matchesif condition:
Add another conditionelif condition:
Handle everything elseelse:
Require both rulesand
Accept either ruleor
Reverse a boolean checknot