Total Pageviews

Wednesday, September 9, 2026

Indentation.

```html

🐍 INDENTATION IN PYTHON

Understanding Python's Block Structure

📘 What is Indentation?

Indentation means adding spaces or tabs at the beginning of a line of code to define a block of statements.

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")
10 is greater than 5

The print() statement is indented, so Python understands that it belongs to the if block.

2️⃣ How Indentation Works

if condition:

Indentation
Statement 1
Statement 2

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 1
Statement 2
💡 Tip: Most Python programmers use 4 spaces for one indentation level.

4️⃣ What Happens Without Indentation?

if 10 > 5:
print("Correct")
IndentationError: expected an indented block

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")
Adult

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")
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)
1
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()
Hello
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")
One
Two

Different indentation levels allow Python to identify nested blocks.

🔟 Same Indentation = Same Block

if True:
    a = 10
    b = 20
    print(a + b)
30

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.
⚠️ Important: Do not mix tabs and spaces inconsistently within the same Python block.

🧱 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

  1. Indentation is mandatory for Python blocks.
  2. Use consistent indentation throughout the program.
  3. Four spaces are conventionally used for one indentation level.
  4. Statements belonging to the same block must have the same indentation.
  5. Nested blocks require additional indentation.
  6. Do not mix tabs and spaces inconsistently.
  7. 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

  1. Python uses indentation to define blocks of code.
  2. Indentation is mandatory in Python.
  3. Four spaces are conventionally used for one indentation level.
  4. Statements in the same block must have the same indentation.
  5. Nested blocks require additional indentation.
  6. Incorrect indentation can produce IndentationError.
  7. Python does not normally use braces { } to define blocks.
  8. 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