Unit 2 · If Statement
If Statement
Learn the foundational building block of conditional decision-making in Python: the basic
if statement, Python's indentation rules, and its execution flow.
Introduction
A computer program normally executes statement by statement, from top to bottom, in a fixed sequence. However, real programs rarely follow a single straight path — they must react differently depending on circumstances. For example:
- Only give a discount if the purchase amount is above ₹1000.
- Only allow login if the password is correct.
- Only print a warning if the battery is below 20%.
- Only send an alert if the temperature exceeds 40°C.
The if statement is Python's most basic decision-making tool. It allows a program to
execute a block of statements only if a given condition evaluates to True.
If the condition is False, the block is simply skipped, and the program continues with
whatever comes next.
Easy Definition
An if statement runs a block of code only when its condition is
True. If the condition is False, the block is skipped entirely.
Real-Life Example
- If it is raining → take an umbrella. (No "else" mentioned — nothing happens otherwise.)
- If the fire alarm rings → evacuate the building.
- If the fuel is below 10% → switch on the warning light.
- If the answer is correct → award marks.
Notice that in each case, action is taken only when the condition is satisfied — no alternative action is described.
Examination Point of View
- Definition and syntax of if statement are frequently asked.
- Rules of indentation in Python (very commonly tested).
- Tracing output of simple if-statement code snippets.
- Identifying IndentationError in faulty code.
Table of Contents
Syntax of If Statement
The syntax of a simple if statement is:
if condition: # statements to execute if condition is True statement_1 statement_2
The statement begins with the keyword if, followed by a conditional expression that
evaluates to True or False, and ends with a colon (:). All
statements inside the conditional block must be indented at the same level.
Important Rules
- The condition must evaluate to a Boolean (True/False) value.
- A colon (
:) is mandatory after the condition. - The indented block defines exactly what belongs to the if statement.
- An if statement can exist completely on its own — an else is not required.
Indentation in Python
Unlike many programming languages (like C, C++, or Java) that use curly braces {} to group
statements, Python uses indentation (spaces or tabs) to define which statements belong
to a block. All statements within the same block must share the same level of indentation — the
standard convention is 4 spaces.
Warning: Mixing spaces and tabs, or
having inconsistent indentation within the same block, will cause an IndentationError.
Correct vs Incorrect Indentation
✅ Correct
if x > 0:
print("Positive")
print("Value accepted")
❌ Incorrect
if x > 0:
print("Positive")
print("Value accepted")
Competitive Exam Notes
- Python enforces indentation as part of its syntax — it is not optional.
- Indentation errors are a very common "spot the error" exam question.
- Most editors use 4 spaces per indentation level by convention (PEP 8).
Logic Flow
When the program encounters an if statement, Python performs the following steps:
- It evaluates the condition expression.
- If the condition is
True, the indented block is executed. - If the condition is
False, the block is skipped, and execution continues with the first unindented statement following the block.
| Condition Result | Indented Block | Rest of Program |
|---|---|---|
True |
Executes | Runs after the block |
False |
Skipped | Runs immediately |
Code Examples
1. Voting Eligibility
age = 20 if age >= 18: print("You are eligible to vote!") print("Make sure to bring your ID.") print("This statement always executes.")
Output
You are eligible to vote! Make sure to bring your ID. This statement always executes.
2. Discount Eligibility (condition is False)
purchase_amount = 700 if purchase_amount > 1000: print("You get a 10% discount!") print("Thank you for shopping.")
Output
Thank you for shopping.
Since purchase_amount (700) is not greater than 1000, the condition is False,
so the discount message is skipped — but the "Thank you" message still prints because it lies
outside the if block.
Real-Life Examples
| Situation | Condition | Action if True |
|---|---|---|
| Low Battery Alert | battery < 20 |
Show warning |
| Overspeed Alert | speed > 80 |
Sound alarm |
| Free Shipping | cart_total >= 500 |
Apply free shipping |
Single-Statement If (Same Line)
If the if block contains only one statement, Python allows writing it on
the same line as the condition — although using a new indented line is generally preferred for
readability.
age = 20 if age >= 18: print("Eligible to vote")
Exam Point
This one-line form is valid Python syntax but is considered poor style (against PEP 8) once more than one statement is involved. Examiners sometimes ask you to identify or rewrite this form.
Common Mistakes
- Forgetting the colon (
:) at the end of the if line. - Inconsistent indentation causing an
IndentationError. - Using
=instead of==inside the condition. - Assuming statements outside the block also depend on the condition — they don't.
- Writing an empty if block without using
pass.
Important Examination Points
- The if statement executes a block only when its condition is True.
- Indentation (not braces) defines a block in Python.
- An if statement does not require an else — it can stand alone.
- Statements outside the indented block always execute regardless of the condition.
- Tracing/output prediction is the most common exam question format for this topic.
Interview Questions
- What is the purpose of the if statement?
- How does Python define a block of code without braces?
- What happens when the if condition evaluates to False?
- What error occurs due to incorrect indentation?
- Can an if block contain multiple statements? How are they identified?
Practice MCQs
1. What is the output?
x = 5
if x > 10:
print("Big")
print("Done")
Answer: Done
2. Which symbol is mandatory at the end of an if condition?
A) semicolon B) colon C) comma D) none
Answer: B) colon
3. What does Python use instead of curly braces to define a block?
Answer: Indentation
Summary
The if statement evaluates a condition and runs its indented block only when that condition
is True. Python relies on strict, consistent indentation instead of braces to define code
blocks — omitting or misaligning it results in an IndentationError. Statements outside the
block always run, regardless of the condition's outcome.