Technical Roadmap

Python Indentation

Indentation refers to the empty spaces at the beginning of a line. In Python is used to tell the interpreter which code block belong together

Every programming language has some set of rules to write code which is called syntax. In Python, one of the most important rules is Indentation.

Why we use Indentation

  • In languages like C, C++, or Java, blocks are defined using curly braces { }. In Python, we use indentation to define these code blocks.
  • Without proper indentation, Python cannot determine which lines of code belong to a specific block (like a loop or a function).
  • If indentation is wrong or missing, Python will throw an IndentationError.
  • Always use spaces (or 1 Tab) per indentation level to maintain clean and readable code.
The Logicoria Tip

Indentation is like grouping lines together so Python knows which statements belong to the same block.

In the code below, we do not use proper indentation; therefore, Python will throw Syntax Error.

PYTHON
x = 10

if x > 5:
print("Greater than 5")  # No indentation
print("Less than 5")     # No indenation

Output

Error

See same code with proper indentation.

PYTHON
x = 10

if x > 5:
    print("Greater than 5")  # Indented 4 spaces
else:
    print("Less than 5")     # Indented 4 spaces

Output

Greater than 5

Quick Revision

  • Indentation = spaces at the beginning of a line
  • Used to define code blocks
  • Python uses indentation instead of { }
  • Correct indentation is mandatory in Python.
  • Incorrect indentation leads to errors.

Related Articles