The IndentationError is one of the most common Python errors for beginners. It occurs when Python encounters incorrect or inconsistent indentation in your code. Unlike most programming languages that use braces {} for code blocks, Python uses indentation to define code structure.
# ❌ Problem
if x > 5:
print("Greater") # Missing indentation!
# ✅ Solution: Add proper indentation (4 spaces recommended)
if x > 5:
print("Greater")
# ✅ Configure your editor:
# - Use 4 spaces per indentation level
# - Convert tabs to spaces
# - Enable "show whitespace" to see indentation
The most common cause - forgetting to indent code after statements that require a code block.
Python 3 doesn't allow mixing tabs and spaces. This often happens when copying code from different sources.
Using different numbers of spaces for indentation at the same level causes this error.
Adding indentation where it's not expected, usually at the start of a file or after a complete statement.
Python requires at least one statement in every code block. Use pass for empty blocks.
def greet(name):
print(f"Hello, {name}") # IndentationError!
if age >= 18:
print("Adult") # IndentationError!
for i in range(5):
print(i) # IndentationError!
def greet(name):
print(f"Hello, {name}") # Properly indented
if age >= 18:
print("Adult") # Properly indented
for i in range(5):
print(i) # Properly indented
def calculate():
x = 10 # 4 spaces
y = 20 # Tab character - IndentationError!
return x + y
def calculate():
x = 10 # 4 spaces
y = 20 # 4 spaces (consistent)
return x + y
# Configure your editor to convert tabs to spaces
# VS Code: "editor.insertSpaces": true
# PyCharm: Settings → Editor → Code Style → Python → Use tab character: unchecked
def process_data():
if True:
print("Start") # 4 spaces
print("Middle") # 2 spaces - IndentationError!
print("End") # 4 spaces
def process_data():
if True:
print("Start") # 4 spaces
print("Middle") # 4 spaces (consistent)
print("End") # 4 spaces
print("Hello") # Unexpected indent at file start!
x = 10
y = 20 # Unexpected indent after complete statement!
print("Hello") # No indentation at file start
x = 10
y = 20 # Same indentation level
def todo_function():
# Will implement later
# IndentationError: expected an indented block
if condition:
# IndentationError!
def todo_function():
# Will implement later
pass # Use pass for empty blocks
if condition:
pass # Placeholder
# Or use ellipsis (Python 3+)
def another_function():
... # Also valid for empty blocks
IndentationError occurs when code has incorrect or inconsistent indentation, such as missing indentation after colons, mixing tabs and spaces, or using inconsistent spacing levels.
Always use spaces (4 spaces per level recommended by PEP 8). Python 3 doesn't allow mixing tabs and spaces. Configure your editor to convert tabs to spaces automatically.
Add proper indentation (4 spaces) after statements that require code blocks (if, for, while, def, class). If the block is empty, use pass or ... as a placeholder.
Enable "show whitespace" or "render whitespace" in your editor settings. In VS Code: View → Render Whitespace. This makes spaces and tabs visible.
Run python -tt yourfile.py to detect tab/space mixing. Use linters like pylint or flake8, or formatters like black or autopep8 to automatically fix indentation.
Explore 500+ free tutorials across 20+ languages and frameworks.