The one thing that makes Python different then most other programming and scripting languages is its strictness towards code indentation. If the code is not well indented, your code will not execute and you will see a IndentationError.
The IndentationError exception class is the base class of syntax errors related to incorrect indentation. It is a subclass of SyntaxError.
To understand it better, one should take a look at the PEP 8 Indentation section.
As per PEP 8 instructions - "We should use 4 spaces per indentation level."
Now let's try to figure out IndentationError: unexpected indent with a simple Python code example.
Example:print("hello there")
print("how are you?")
Exception:
File "<ipython-input-16-2298b55469ab>", line 2
print("how are you?")
^
IndentationError: unexpected indent
Why unexpected indent exception?
In Python, we should add an indentation (4 spaces per indentation level per PEP 8) to indicate the beginning or ending of code blocks, such as,
For Loops
Example:
numbers = [84, 87, 76, 92, 14, 87, 98]
for number in numbers:
print(number) # 4 space indentation!
While Loops
Example:
numbers = [84, 87, 76, 92, 14, 87, 98]
index = 0
while index < len(numbers):
print(numbers[index]) # 4 space indentation! for the block!
index += 1
If-Else Conditional Statements
Example:
numbers = [84, 87, 76, 92, 14, 87, 98]
for number in numbers:
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
- Function definition
Example:
def print_even_odd(numbers):
for number in numbers:
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
numbers = [84, 87, 76, 92, 14, 87, 98]
print_even_odd(numbers)
Try-Except Blocks
Example:
def print_even_odd(numbers):
try:
for number in numbers:
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
except TypeError:
print("Error: The input should be a list of numbers")
numbers = [84, 87, 76, 92, 14, 87, 98]
print_even_odd(numbers)
As you would have understood, the two lines we have is neither among the above cases, thus the second line cannot have a intention, thus causing IndentationError: unexpected indent
-
In Python, we should add an indentation (4 spaces per indentation level per PEP 8) to indicate the beginning or ending of code blocks, such as,
For Loops
Example:numbers = [84, 87, 76, 92, 14, 87, 98] for number in numbers: print(number) # 4 space indentation!
While Loops
Example:numbers = [84, 87, 76, 92, 14, 87, 98] index = 0 while index < len(numbers): print(numbers[index]) # 4 space indentation! for the block! index += 1
If-Else Conditional Statements
Example:numbers = [84, 87, 76, 92, 14, 87, 98] for number in numbers: if number % 2 == 0: print(f"{number} is even") else: print(f"{number} is odd")
- Function definition
Example:
def print_even_odd(numbers): for number in numbers: if number % 2 == 0: print(f"{number} is even") else: print(f"{number} is odd") numbers = [84, 87, 76, 92, 14, 87, 98] print_even_odd(numbers)
Try-Except Blocks
Example:def print_even_odd(numbers): try: for number in numbers: if number % 2 == 0: print(f"{number} is even") else: print(f"{number} is odd") except TypeError: print("Error: The input should be a list of numbers") numbers = [84, 87, 76, 92, 14, 87, 98] print_even_odd(numbers)
As you would have understood, the two lines we have is neither among the above cases, thus the second line cannot have a intention, thus causing IndentationError: unexpected indent

Facing issues? Have Questions? Post them here! I am happy to answer!
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
- Java Date Time API: LocalDateTime get(TemporalField field) examples - Java
- Java 8 JDBC: Insert Timestamp Code Example - Java
- [macOS] Fix: MySQL ERROR 2002 (HY000): Cant connect to local MySQL server through socket /tmp/mysql.sock (2) - MySQL
- Java: Convert Double to 2 Decimal Places [Examples] - Java
- MySQL : Error :1004 SQLSTATE: HY000 (ER_CANT_CREATE_FILE) Can't create file - MySQL
- Split String in Python with Multiple Delimiters - Python
- Install SonarLint on Visual Studio Code - HowTos
- Parsing Data for android-21 failed unsupported major.minor version 51.0 - Android