✅ Python if Statement – Syntax, Examples & Best Practices
🧲 Introduction
In every real-world program, decision-making is fundamental. Whether you’re validating a login, filtering data, or toggling between UI themes, the if statement in Python allows you to control the flow of execution based on conditions. It’s Python’s primary control flow structure, enabling your code to respond intelligently to data and user input.
🔑 Syntax of the if Statement
if condition:
# block of code to execute if condition is True
✅ Explanation:
- The
conditionmust evaluate to a Boolean (TrueorFalse). - If
True, the indented block runs. - If
False, Python skips the block.
🧪 Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
✅ Output:
You are eligible to vote.
💡 Tip: Use consistent indentation. Python uses whitespace (usually 4 spaces) to define blocks—not braces like in C or Java.
🔁 if...else – Binary Decision
if condition:
# block if condition is True
else:
# block if condition is False
🧪 Example:
a = int(input("Enter a number: "))
if a > 0:
print("Positive number")
else:
print("Non-positive number")
✅ Output (if input is -2):
Non-positive number
📘 Best Practice: Use else when you want an alternative action if the initial condition fails.
🔁 if...elif...else – Multiple Conditions
if condition1:
# block 1
elif condition2:
# block 2
elif condition3:
# block 3
else:
# default block
🧪 Example:
x = "python"
if x == "java":
print("Java")
elif x == "python":
print("Python")
else:
print("Unknown")
✅ Output:
Python
💡 Tip: Use elif when you want to check multiple exclusive conditions.
🧱 Nested if Statements
You can nest if statements inside each other.
🧪 Example:
x = 10
if x > 0:
if x % 2 == 0:
print("Positive even number")
✅ Output:
Positive even number
⚠️ Warning: Overusing nesting can make code hard to read. Consider flattening logic or using functions.
🔥 Common Mistakes with if
| ❌ Mistake | ⚠️ Description |
|---|---|
Forgetting the colon : | Most frequent syntax error |
| Incorrect indentation | Causes IndentationError |
Using = instead of == | Assignment vs comparison |
| Not using Boolean logic properly | Eg: if x = 5 instead of if x == 5 |
🧪 Bad Code:
if x = 5: # SyntaxError
print("x is 5")
✅ Correct:
if x == 5:
print("x is 5")
🧮 Truthy and Falsy Values in if
Python treats some values as False in conditionals:
0,0.0,'',[],{},None,False⟶ Falsy- Everything else ⟶ Truthy
🧪 Example:
if []:
print("This won’t print")
else:
print("Empty list is falsy")
✅ Output:
Empty list is falsy
🧠 Summary – Key Takeaways
ifcontrols decision-making in Python.- Use
if,elif, andelseto handle multiple branches. - Python uses indentation instead of braces.
- Avoid common pitfalls like forgetting the colon or misusing
==.
❓ FAQ – Python if Statement
❓What is the correct syntax for an if statement in Python?
if condition:
# indented block
The condition must evaluate to a Boolean value.
❓Can I use multiple elif blocks?
Yes, you can have as many elif clauses as needed. Only the first True condition executes.
❓Is indentation mandatory in Python?
Yes, indentation is part of Python’s syntax. All statements in a block must align equally.
❓Can I use logical operators in if conditions?
Absolutely. Use and, or, and not to combine multiple conditions:
if age > 18 and citizen:
print("Eligible")
❓Is switch-case available in Python?
No, Python doesn’t have switch-case. Use if-elif-else or match-case (Python 3.10+) as alternatives.
Share Now :
