If-Else Statements in Python
The if-else statement is one of the most important tools in programming. It lets your code make decisions: do one thing if a condition is true, and something different if it's false.
Think of it like real-life choices: if it's raining, take an umbrella; else, wear sunglasses. In code, this helps create programs that respond to different situations – like checking if a player won a game or if a password is correct.
We'll start with the basic if-else, then add elif for more options, look at practical examples, common mistakes, and give you plenty of practice.
Basic if-else Syntax
The structure is simple: if (condition): do this, else: do that. The colon : is required, and indentation defines the blocks.
If the condition is True, the if block runs. If False, the else block runs – exactly one of them will execute.
You can have as many lines as you want in each block – just keep the indentation consistent.
Real-World Examples
Let's look at some everyday situations to see how useful if-else is.
Example 1: Even or odd number
Example 2: Login check
Example 3: Simple game score
Using elif for Multiple Conditions
When you have more than two possibilities, use elif (else if). Python checks conditions from top to bottom and runs only the first true block.
This is cleaner than many nested if-else statements.
You can have as many elif blocks as needed. The final else is optional – it runs if nothing else matched.
Common Mistakes to Avoid
- Forgetting the colon : after if/elif/else
- Wrong indentation – blocks must be aligned
- Using = (assignment) instead of == (comparison)
- Forgetting parentheses in complex conditions
- Putting code outside the block by mistake
Quick Quiz
Az anyagokat átnéztük és ellenőriztük, de hibák továbbra is előfordulhatnak. A tartalom kizárólag oktatási célt szolgál, ezért saját felelősségre használd, és szükség esetén ellenőrizd más forrásokkal is.
✨ Kérdezd Larát — a tanulási partnered
Fedezd fel a személyre szabott tanulási támogatást. Lara elmagyarázza az anyagot, összefoglalja a témákat és megválaszolja a kérdéseidet — az Go csomagtól elérhető.
Lara segít gyorsabban tanulni — kizárólag a ReadyTools Go, Plus és Max tagoknak.

