
📎 This article includes 2 downloadable practice files ↓
In this article
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.
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.
Stuck on a step? Ask a question and the AI answers using this article.