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
We have reviewed and checked the materials, but errors may still occur. The content is provided for educational purposes only, so use it at your own responsibility and verify with other sources if needed.
✨ Ask Lara — your AI study partner
Unlock personalized learning support. Lara can explain lessons, summarize topics, and answer your study questions — available from the Go plan and above.
Lara helps you learn faster — exclusive to ReadyTools Go, Plus, and Max members.


