Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

IndentationError in Python — How to Fix (2026) | Tutorials Logic

What is This Error?

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.

Common Causes

  • Missing indentation after colon (:) in if, for, while, def, class statements
  • Mixing tabs and spaces for indentation
  • Inconsistent indentation levels (2 spaces vs 4 spaces)
  • Extra or unexpected indentation
  • Copy-pasting code with different indentation styles

Quick Fix (TL;DR)

Quick Solution
# ❌ 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

Common Scenarios & Solutions

Scenario 1: Missing Indentation After Colon

The most common cause - forgetting to indent code after statements that require a code block.

Problem
def greet(name):
print(f"Hello, {name}")  # IndentationError!

if age >= 18:
print("Adult")  # IndentationError!

for i in range(5):
print(i)  # IndentationError!
Solution
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

Scenario 2: Mixing Tabs and Spaces

Python 3 doesn't allow mixing tabs and spaces. This often happens when copying code from different sources.

Problem
def calculate():
    x = 10  # 4 spaces
	y = 20  # Tab character - IndentationError!
    return x + y
Solution
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

Scenario 3: Inconsistent Indentation Levels

Using different numbers of spaces for indentation at the same level causes this error.

Problem
def process_data():
    if True:
        print("Start")  # 4 spaces
      print("Middle")  # 2 spaces - IndentationError!
        print("End")    # 4 spaces
Solution
def process_data():
    if True:
        print("Start")  # 4 spaces
        print("Middle")  # 4 spaces (consistent)
        print("End")    # 4 spaces

Scenario 4: Unexpected Indent

Adding indentation where it's not expected, usually at the start of a file or after a complete statement.

Problem
    print("Hello")  # Unexpected indent at file start!

x = 10
    y = 20  # Unexpected indent after complete statement!
Solution
print("Hello")  # No indentation at file start

x = 10
y = 20  # Same indentation level

Scenario 5: Empty Code Block

Python requires at least one statement in every code block. Use pass for empty blocks.

Problem
def todo_function():
    # Will implement later
# IndentationError: expected an indented block

if condition:
# IndentationError!
Solution
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

Best Practices to Avoid This Error

  • Use 4 spaces per indentation level - PEP 8 standard for Python code
  • Never mix tabs and spaces - Configure your editor to convert tabs to spaces
  • Enable "show whitespace" in your editor - Makes indentation visible
  • Use a Python-aware editor - VS Code, PyCharm, or Sublime with Python plugins
  • Run python -tt script.py - Detects tab/space mixing issues
  • Use autopep8 or black - Auto-format your code to fix indentation
  • Be consistent - Stick to one indentation style throughout your project

Related Errors

Key Takeaways
  • IndentationError occurs when Python encounters incorrect or inconsistent indentation
  • Python uses indentation (not braces) to define code blocks
  • Use 4 spaces per indentation level as per PEP 8 standard
  • Never mix tabs and spaces - configure your editor to use spaces only
  • Every statement ending with colon (:) requires an indented block below it
  • Use pass or ... for empty code blocks that you'll implement later

Frequently Asked Questions


Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.