Decision Making in Python (if-else)
Normally, Python executes your code line-by-line from top to bottom. But real-world software needs to make decisions based on rules.
For example:
- If a student gets 35 marks or more → They Pass.
- Else (otherwise) → They Fail.
To achieve this, we use Conditional Statements.
1. What is a "Condition" vs a "Conditional Statement"?
Before writing code, let's clarify two core terms:
- Condition: A logical test that evaluates to either
TrueorFalse(e.g.,marks >= 35). - Conditional Statement: The decision-making construct (
if,if-else) that controls which block of code to run based on the condition.
2. Visual Structure: Anatomy of an if Statement
Understanding how an if statement is structured visually helps prevent syntax and indentation errors:
Syntax Structure of an if Statement
3. The 3 Golden Rules of if Syntax
- The Condition: The logical test that produces
TrueorFalse. - The Colon (
:): Placed at the end of theifline. It signals to Python: "Attention! A new block of code starts on the next line." - Indentation (4 Spaces): Spacing in front of the conditional block. It is used to identify which lines belong inside the conditional block.
4. The Simple if Statement (One Choice)
The if statement checks a condition. If the condition is True, the conditional block runs. If it is False, Python completely skips it.
marks = 75
if marks >= 35:
print("Congratulations!")
print("You passed the exam.")
print("Program complete.")
Output:
Congratulations!
You passed the exam.
Program complete.
5. The if-else Statement (Two Choices)
What if you want one action when the condition is True, and a fallback action when it is False? We add an else block.
Syntax Structure of an if-else Statement
marks = 28
if marks >= 35:
print("Result: Pass")
else:
print("Result: Fail")
How it executes:
- If
marks >= 35isTrue, only theifblock runs. - If
marks >= 35isFalse, only theelseblock runs. - Notice that both
ifandelseend with a colon (:), and their blocks are indented by 4 spaces.
6. What is Indentation & Possible Mistakes
In languages like C, Java, or JavaScript, curly braces { } are used to group code. Python does not use curly braces. Instead, Python uses Indentation (spaces) to define a code block.
marks = 80
if marks >= 35:
print("Line 1: Inside IF block") # Indented -> Inside IF
print("Line 2: Inside IF block") # Indented -> Inside IF
print("Line 3: Outside IF block") # NOT indented -> Always runs
Common Mistakes to Avoid:
❌ Mistake 1: Missing Colon (:)
if marks >= 35 # Missing colon at the end!
print("Pass")
# SyntaxError: expected ':'
❌ Mistake 2: Missing Indentation
if marks >= 35:
print("Pass") # Not indented!
# IndentationError: expected an indented block after 'if' statement
Quick Summary
- Condition Check: Evaluates an expression to either
TrueorFalse. - The Colon (
:): Mandatory at the end of everyifandelseheader line. - Indentation (4 spaces): Python uses indentation to group statements inside the conditional block.
- The
elseBlock: Executes as the default fallback when theifcondition isFalse.
What's Next?
Now that you understand basic if-else logic, let's learn how to handle multiple choices using elif chains in the next lesson!