What is unexpected indent in Python?
An "IndentationError: unexpected indent" in Python occurs when there is an incorrect or unexpected indentation in your code. Python relies on indentation to determine the structure and nesting of code blocks, such as loops, conditionals, and function definitions. If the indentation does not match the expected structure, Python raises this error. Let's see some examples and how to resolve them:
Incorrect Indentation
In this example, the indentation level of the print(i) statement is one level deeper than the for loop. Python expects statements inside a loop to be indented, but the print(i) statement is not indented correctly, leading to the "IndentationError: unexpected indent" error.
To fix this, ensure that all statements inside the for loop have the same indentation:
Mixing Tabs and Spaces
In this example, the print(i) statement appears to be indented with a mix of tabs and spaces. Python requires consistent indentation, either tabs or spaces, but not both. Mixing tabs and spaces can lead to this indentation error.
To resolve this issue, choose one indentation style and apply it consistently throughout your code:
Incorrectly Aligned Code Block
In this example, the if statement is not properly indented under the function definition. Python expects the code block inside the function to be indented, but the if statement is not aligned correctly.
To fix this, ensure that the code block inside the function is consistently indented:

Python Indentation
Most programming languages permit indentation , but don't enforce it. Python enforces it with an iron fist. This is different from many other programming languages that use curly braces {} to delimit blocks such as C, C++, Java and Javascript. Because of this, Python users must pay close attention to when and how they indent their code because whitespace matters. Python's use of indentation comes directly from ABC . ABC is an interactive programming language and environment for personal computing, originally intended as a good replacement for BASIC .
Conclusion
An "IndentationError: unexpected indent" in Python can be resolved by carefully inspecting and correcting the indentation levels in your code. Ensure that code blocks are consistently indented, avoid mixing tabs and spaces, and make sure that statements inside loops, conditionals, or function definitions are appropriately aligned. Following these practices will help you avoid the "IndentationError" and maintain a clear and readable Python codebase.
- TypeError: 'NoneType' object is not subscriptable
- IndexError: string index out of range
- ValueError: too many values to unpack (expected 2)
- SyntaxError- EOL while scanning string literal
- TypeError: Can't convert 'int' object to str implicitly
- IndentationError: expected an indented block
- ValueError: invalid literal for int() with base 10
- IndexError: list index out of range : Python
- AttributeError: 'module' object has no attribute 'main'
- UnboundLocalError: local variable referenced before assignment
- TypeError: string indices must be integers
- FileNotFoundError: [Errno 2] No such file or directory
- Fatal error: Python.h: No such file or directory
- ZeroDivisionError: division by zero
- ImportError: No module named requests | Python
- TypeError: 'NoneType' object is not iterable
- SyntaxError: unexpected EOF while parsing | Python
- zsh: command not found: python
- Unicodeescape codec can't decode bytes in position 2-3
- The TypeError: 'tuple' object does not support item assignment
- The AttributeError: 'bytes' object has no attribute 'read'