Understanding Indentation in Python
Understanding Indentation in Python
Indentation is one of the most important aspects of Python syntax. Unlike other programming languages that use curly braces {} or keywords to define code blocks, Python relies on whitespace (indentation) to determine the structure of the code.
1. What is Indentation?
Indentation refers to the spaces or tabs placed at the beginning of a line of code. It is used to define blocks of code, such as functions, loops, and conditional statements.
Example:
if True:
print("This is an indented block") # Correct indentation
If indentation is missing, Python will raise an error.
Incorrect example:
if True:
print("This will cause an error") # No indentation
Error:
IndentationError: expected an indented block
2. Indentation Rules in Python
- Consistency is Key: Indentation must be consistent within a block.
- Use 4 Spaces per Indentation Level (Recommended by PEP 8, Python’s style guide).
- Tabs and Spaces Should Not Be Mixed in the same script.
Correct example:
def greet():
print("Hello, World!") # 4 spaces used for indentation
Incorrect example:
def greet():
print("Hello, World!") # Uses a tab instead of spaces (Not recommended)
Mixing spaces and tabs may cause an IndentationError in Python.
3. Indentation in Control Structures
Python requires indentation for code blocks inside control structures like if-else, loops, and functions.
Indentation in Conditional Statements
age = 18
if age >= 18:
print("You are an adult.") # Indented block
else:
print("You are a minor.") # Indented block
Indentation in Loops
for i in range(3):
print("Iteration:", i) # Indented block
Indentation in Functions
def say_hello():
print("Hello!") # Indented block
say_hello()
4. Nested Indentation
When using nested loops or conditions, the indentation increases for each level.
Example:
for i in range(3):
print("Outer loop:", i) # First level indentation
for j in range(2):
print(" Inner loop:", j) # Second level indentation
Output:
Outer loop: 0
Inner loop: 0
Inner loop: 1
Outer loop: 1
Inner loop: 0
Inner loop: 1
Outer loop: 2
Inner loop: 0
Inner loop: 1
5. Common Indentation Errors and Fixes
Error 1: Missing Indentation
if True:
print("Hello, Python!") # No indentation
Error:
IndentationError: expected an indented block
Fix:
if True:
print("Hello, Python!") # Correct indentation
Error 2: Inconsistent Indentation
def test():
print("Line 1")
print("Line 2") # Mixing spaces and tabs
Error:
TabError: inconsistent use of tabs and spaces in indentation
Fix:
def test():
print("Line 1")
print("Line 2") # Consistent indentation (4 spaces)
6. Best Practices for Indentation
Always use 4 spaces per indentation level (recommended by PEP 8).
Do not mix tabs and spaces in the same file.
Use an editor that supports automatic indentation (VS Code, PyCharm).
If working in a team, follow consistent indentation rules.
Conclusion
Indentation in Python is not just for readability—it is a syntax requirement. Proper indentation ensures clear, structured, and error-free code. By following Python’s indentation rules, you can write clean and maintainable programs.