Python Lesson 3: Decisions With if, elif and else

📎 This article includes 2 downloadable practice files ↓

⏱ 2 min read

📘 Python Beginner Course · Lesson 3 of 12

In this article
  1. if, elif, else
  2. Comparisons and combining
  3. One-line version
  4. Truthiness
  5. Where beginners go wrong
  6. Practice

Excel’s IF becomes Python’s if. The big difference: Python uses indentation (spaces at the start of lines) to know which lines belong to the decision.

if, elif, else

sales, target = 505000, 450000

if sales >= target * 1.1:
    rating = "A"
elif sales >= target:
    rating = "B"
else:
    rating = "C"
print("Rating:", rating)

The colon ends each condition; the indented lines (4 spaces) run only when it’s true. elif means “else if”. Python checks top to bottom and stops at the first true condition, just like nested IF.

Comparisons and combining

Python Means
== / != Equal / not equal (one = assigns, two compare)
< <= > >= As in Excel
and / or / not AND() / OR() / NOT()
in region in ("North", "East")
if sales >= target and days_absent <= 2:
    print("Star performer")

One-line version

result = "Pass" if marks >= 40 else "Fail"
discount = 0.08 if amount >= 100000 else 0.05 if amount >= 50000 else 0.02

Good for short choices; use a full if/elif block when it gets longer than one readable line.

💡 VS Code inserts 4 spaces when you press Tab in a .py file. Never mix tabs and spaces; Python refuses to run mixed indentation.

Truthiness

Empty text "", 0, empty lists and None count as false. if customer: means “if customer isn’t empty”.

Where beginners go wrong

Mistake Error / effect
Missing colon SyntaxError: expected ‘:’
Wrong indentation IndentationError, or code running at the wrong time
if x = 5 Use == to compare
Comparing “500” (text) with 400 TypeError; convert with int() first

Practice

Download the script (and data file, if listed) below into one folder. Open a terminal in that folder and run python lesson-NN.py. Compare with the expected output, then change something and run it again: that’s how Python is learned.

📎 Practice files for this article

  • 🐍
    Lesson 3 scriptThe complete script from this lesson, tested.
    ⬇ PY · 519 B
  • 📄
    Expected outputWhat the script prints when you run it.
    ⬇ TXT · 58 B

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong