Python if...else Statement – Binary Control Flow Explained
Introduction – Why if...else Matters
Real-world programs often need to make binary decisions—if something is true, do one thing; otherwise, do another. Python’s if...else structure helps you implement this logic efficiently.
For example:
- If a user is logged in, show the dashboard.
- Else, redirect to the login page.
Let’s explore how to write and use if...else statements in Python with real-world clarity.
Syntax of if...else
if condition:
# code block if condition is True
else:
# code block if condition is False
- The
ifevaluates a condition. - If it’s
True, the indented block underifruns. - Otherwise, the
elseblock runs.
Note: Use proper indentation—Python relies on it!
Example: Even or Odd Number Checker
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Output (if num = 7)
The number is odd.
Explanation:
- The condition
num % 2 == 0checks for evenness. - If
True, it prints “even”, otherwise “odd”.
Example: Simple Login Check
username = input("Enter username: ")
if username == "admin":
print("Welcome, admin!")
else:
print("Access denied.")
Output:
Access denied.
Best Practice: Use == for comparison, not = (which is assignment).
Nested if...else Example
age = int(input("Enter your age: "))
if age >= 18:
if age >= 65:
print("You are a senior citizen.")
else:
print("You are an adult.")
else:
print("You are a minor.")
Output (if age = 70):
You are a senior citizen.
Tip: Avoid excessive nesting for readability. Use elif if appropriate.
Common Errors and Fixes
| Error | Reason | Fix |
|---|---|---|
if x = 5: | Uses = instead of == | if x == 5: |
| Missing colon | SyntaxError | Add : after condition |
| Incorrect indentation | IndentationError | Indent blocks consistently |
| Unreachable else block | Condition is always True or False | Re-check logic |
Comparison Table: if vs if...else
| Feature | if Only | if...else |
|---|---|---|
| Condition True | Executes code block | Executes if block |
| Condition False | Skips the block (does nothing) | Executes else block |
| Binary Logic | No | Yes |
| Suitable For | Optional execution | Yes/No decisions |
Summary – Python if...else Statement
- Purpose: To control the flow of a program by executing one block of code when a condition is
True, and another when it’sFalse. - Syntax:
if condition:
# run if true
else:
# run if falseKey Takeaways
if...elseenables binary decisions in your Python program.- The
elseblock runs only if theifcondition fails. - Python depends on indentation—not braces
{}—to define blocks. - Use with logical operators like
and,or,notfor compound conditions.
Real-World Relevance
- Used in authentication, form validations, workflow branching, and more.
- Foundation for more advanced decision-making tools like
elifandmatch-case.
FAQ – Python if...else Statement
What is the purpose of else in Python?
else provides an alternate code path when the if condition evaluates to False.
Can I use else without if?
No. else must always follow an if (or elif) statement.
Can I put if...else on one line?
Yes, but it should only be used for simple expressions:
print("Even") if num % 2 == 0 else print("Odd")
Share Now :
