Python relies heavily on indentation to define blocks of code. Unlike other languages that use {}
(curly braces), Python uses whitespace (spaces or tabs) to determine where a block of code starts and ends. Incorrect indentation inside loops can cause IndentationErrors, unexpected logic errors, or infinite loops.
In this guide, we’ll go step by step to understand how incorrect indentation affects loops and how to fix it.
1. Understanding Indentation in Python
In Python, code blocks inside loops, functions, and conditionals must be indented consistently.
Correct Example:
for i in range(5):
print(i) # This is correctly indented
- The print statement is indented inside the loop, so it runs five times.
Incorrect Example (Missing Indentation):
for i in range(5):
print(i) # IndentationError: expected an indented block
- Since
print(i)
is not indented, Python does not recognize it as part of the loop, causing anIndentationError
.
2. Common Indentation Mistakes in Loops
Mistake 1: Missing Indentation in Loops
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num) # IndentationError
Fix:
for num in numbers:
print(num) # Correct indentation
Mistake 2: Extra Indentation Outside Loop
for i in range(3): # Unexpected Indentation
print(i)
Error:
IndentationError: unexpected indent
Fix:
for i in range(3):
print(i)
Mistake 3: Inconsistent Indentation (Mixing Tabs and Spaces)
for i in range(3):
print(i) # Using a tab
print("Python") # Using spaces
Error:
TabError: inconsistent use of tabs and spaces in indentation
Fix: Use only spaces (Python recommends 4 spaces per indent).
for i in range(3):
print(i)
print("Python")
Mistake 4: Incorrect Indentation in Nested Loops
for i in range(3):
for j in range(2):
print(i, j) # ❌ IndentationError
Fix:
for i in range(3):
for j in range(2):
print(i, j) # Properly indented
3. How Indentation Affects Loop Logic
Incorrect indentation may not always cause an error, but it can lead to unexpected behavior.
Example: Wrong Indentation Causes Incorrect Loop Execution
for i in range(3):
print("Start of loop")
print(i) # Runs only once instead of inside the loop
Unexpected Output:
Start of loop
Start of loop
Start of loop
2 # Printed once, outside the loop
Fix:
for i in range(3):
print("Start of loop")
print(i) # Properly indented
Correct Output:
Start of loop
0
Start of loop
1
Start of loop
2
4. Indentation in while
Loops
Mistake: Incorrect Indentation Causing Infinite Loop
i = 0
while i < 3:
print(i) # IndentationError
i += 1 # This is also not inside the loop
Fix:
i = 0
while i < 3:
print(i)
i += 1 # Properly indented inside the loop
5. Best Practices to Avoid Indentation Errors
- Use a Consistent Indentation Style (Python recommends 4 spaces).
- Never Mix Tabs and Spaces – Use spaces instead of tabs.
- Use an IDE or Code Editor That Highlights Indentation Errors (e.g., VS Code, PyCharm).
- Manually Check Indentation for Nested Loops and Conditionals.
- Avoid Accidental Extra Indents (Remove unnecessary spaces before
for
,while
,if
).
6. Summary of Common Indentation Mistakes
Mistake | Error Type | Fix |
---|---|---|
Missing indentation inside loops | IndentationError: expected an indented block | Indent loop body correctly |
Extra indentation before a loop | IndentationError: unexpected indent | Remove unnecessary spaces |
Mixing spaces and tabs | TabError: inconsistent use of tabs and spaces | Use only spaces |
Incorrect indentation in nested loops | IndentationError | Ensure proper indentation of inner loops |
Indentation outside loops affecting execution | Wrong logic | Verify indentation carefully |