🐍 INDENTATION IN PYTHON
Understanding Python's Block Structure
📘 What is Indentation?
In Python, indentation is not optional. It is used to identify which statements belong to a particular block such as if, else, for, while, function, class, etc.
1️⃣ Simple Example
if 10 > 5:
print("10 is greater than 5")
The print() statement is indented, so Python understands
that it belongs to the if block.
2️⃣ How Indentation Works
Indentation
All statements having the same indentation level belong to the same block.
3️⃣ Standard Indentation
Python's recommended standard is 4 spaces for each indentation level.
if True:
print("Statement 1")
print("Statement 2")
Statement 2
4️⃣ What Happens Without Indentation?
if 10 > 5:
print("Correct")
Python generates an IndentationError because the
statement inside the if block is not indented.
5️⃣ Indentation with if-else
age = 20
if age >= 18:
print("Adult")
else:
print("Minor")
Notice that both print() statements are indented inside
their respective blocks.
6️⃣ Indentation in Nested Statements
age = 20
if age >= 18:
if age >= 60:
print("Senior Citizen")
else:
print("Adult")
Here there are two indentation levels:
| Level | Statement |
|---|---|
| Level 0 | if age >= 18: |
| Level 1 | if age >= 60: |
| Level 2 | print("Senior Citizen") |
7️⃣ Indentation with for Loop
for i in range(1, 4):
print(i)
2
3
The indented print(i) statement executes during each
iteration of the loop.
8️⃣ Indentation in Functions
def greet():
print("Hello")
print("Welcome to Python")
greet()
Welcome to Python
The two print() statements form the body of the function
because they are indented.
9️⃣ Multiple Indentation Levels
for i in range(1, 3):
if i == 1:
print("One")
else:
print("Two")
Two
Different indentation levels allow Python to identify nested blocks.
🔟 Same Indentation = Same Block
if True:
a = 10
b = 20
print(a + b)
All three statements have the same indentation and therefore belong
to the same if block.
⚠️ Spaces vs Tabs
| Method | Description |
|---|---|
| Spaces | Recommended approach; normally 4 spaces per level. |
| Tabs | Can be used, but mixing tabs and spaces can cause errors. |
🧱 Python Block Structure
if condition:
statement 1
statement 2
statement 3
statement 4
Here, statements 1, 2 and 3 are inside the if block,
while statement 4 is outside the block.
✅ Advantages of Indentation
- Makes Python code easy to read.
- Clearly defines blocks of code.
- Improves program structure.
- Reduces the need for braces such as
{ }. - Makes nested statements easier to understand.
- Encourages clean and consistent programming.
- Helps identify the scope of statements visually.
❌ Problems Caused by Incorrect Indentation
- Can produce
IndentationError. - Incorrect indentation can change program logic.
- Mixing tabs and spaces may cause errors.
- Beginners may initially find indentation confusing.
- Moving a statement to the wrong level can change its block.
📏 Rules of Indentation in Python
- Indentation is mandatory for Python blocks.
- Use consistent indentation throughout the program.
- Four spaces are conventionally used for one indentation level.
- Statements belonging to the same block must have the same indentation.
- Nested blocks require additional indentation.
- Do not mix tabs and spaces inconsistently.
- A colon
:generally introduces a new indented block.
🚨 Common Indentation Errors
| Error | Reason |
|---|---|
IndentationError |
Expected indentation is missing. |
| Unexpected indentation | A line has more indentation than expected. |
| Inconsistent indentation | Different indentation styles are used incorrectly. |
🎯 Important Points for Examination
- Python uses indentation to define blocks of code.
- Indentation is mandatory in Python.
- Four spaces are conventionally used for one indentation level.
- Statements in the same block must have the same indentation.
- Nested blocks require additional indentation.
- Incorrect indentation can produce
IndentationError. - Python does not normally use braces
{ }to define blocks. - Do not mix tabs and spaces inconsistently.
📌 Quick Summary
| Concept | Meaning |
|---|---|
| Indentation | Spaces/tabs at the beginning of a line. |
| Purpose | Defines blocks of Python code. |
| Recommended | 4 spaces per indentation level. |
| Same level | Statements belong to the same block. |
| Nested block | Requires additional indentation. |
| Wrong indentation | May produce IndentationError or change program logic. |
No comments:
Post a Comment