Skip to content

Block Indentation in Python

Python uses indentation to define the structure and flow of code blocks. Indentation is crucial for defining the start and end of blocks such as loops, conditionals, functions, and classes. Consistent indentation is essential for the correct execution of a Python program.

1. Importance of Indentation

In Python, indentation is not just for readability; it is part of the syntax. Each indentation level is typically 4 spaces.

Example:

# Correct indentation
if True:
    print("This is correctly indented")

# Incorrect indentation
if True:
print("This will cause an IndentationError")

2. Indentation in Control Structures

2.1 If-Else Statements

Indentation is used to define the code blocks for if, elif, and else statements.

Example:

x = 10

if x > 5:
    print("x is greater than 5")
else:
    print("x is 5 or less")

Output:

x is greater than 5

2.2 Loops

Indentation is also used to define the code blocks within loops.

Example with for loop:

for i in range(5):
    print(i)
    if i == 3:
        print("Reached 3")

Output:

0
1
2
3
Reached 3
4

Example with while loop:

i = 0
while i < 5:
    print(i)
    i += 1

Output:

0
1
2
3
4

3. Indentation in Functions

Function definitions and the code within functions are also defined by indentation.

Example:

def greet(name):
    print(f"Hello, {name}!")
    if name == "Alice":
        print("Welcome back, Alice!")
    else:
        print("Nice to meet you!")

greet("Alice")
greet("Bob")

Output:

Hello, Alice!
Welcome back, Alice!
Hello, Bob!
Nice to meet you!

4. Indentation in Classes

Class definitions and methods within classes are indented similarly.

Example:

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} makes a sound")

class Dog(Animal):
    def speak(self):
        print(f"{self.name} barks")

dog = Dog("Buddy")
dog.speak()

Output:

Buddy barks

5. Nesting and Indentation

Nested structures require additional levels of indentation.

Example:

x = 10

if x > 5:
    print("x is greater than 5")
    for i in range(3):
        print(i)
        if i == 2:
            print("Reached 2")
else:
    print("x is 5 or less")

Output:

x is greater than 5
0
1
2
Reached 2

6. Common Indentation Errors

Example:

x = 10

if x > 5:
print("This will cause an IndentationError")  # Incorrect indentation

Error Message:

IndentationError: expected an indented block

Conclusion

Indentation in Python is crucial for defining the structure and flow of your code. Proper indentation enhances readability and ensures that your code executes correctly. By practicing the examples provided, you can gain a deeper understanding of how indentation works in Python and how to apply it effectively in your projects.