Back to Python Notes
Topic #213

Python Indentation

Understand how Python uses indentation to define code blocks and why consistent whitespace is critical for program structure.

What it is

In many programming languages like C, Java, or JavaScript, curly braces {} define the scope of a block of code. Python takes a different approach: it uses indentation (whitespace at the beginning of a line) to determine which statements belong together. This concept is known as "significant whitespace."

The mental model is simple: if lines are indented to the same level, they form a single logical block. If you increase the indentation, you are entering a new nested block. Common related terms include block, suite, and scope.

Why it matters

  • Readability: The visual structure of the code matches its logical flow, making complex algorithms easier to scan.
  • Error Prevention: It eliminates the ambiguity of missing or misplaced braces, which can cause subtle bugs in other languages.
  • Standardization: Following PEP 8 (the official style guide) ensures your code looks familiar to other Python developers.
  • Simplicity: You do not need to write extra characters like semicolons or braces to close blocks.

Syntax or steps

A block begins with a colon : at the end of a header line (such as if, for, def, or class). The next line must be indented. By convention, use 4 spaces per indentation level. Do not mix tabs and spaces.

  1. Write the control statement ending with a colon.
  2. Press Enter and indent the cursor by 4 spaces.
  3. Write the body of the block.
  4. Dedent (move back to the left margin) to exit the block.

Example

x = 10

if x > 0:
    print("positive")
    if x > 5:
        print("greater than 5")
else:
    print("non-positive")

print("done")

Explanation:

  • if x > 0: starts the first block. The two print statements below it are indented, so they only run if x is positive.
  • The inner if x > 5: is further indented, creating a nested block inside the first one.
  • else: aligns with the initial if, indicating an alternative path.
  • print("done") has no indentation, so it runs regardless of the conditions above.

Common mistakes

  • Mixing Tabs and Spaces: Python 3 disallows this. Configure your editor to insert spaces when you press Tab.
  • Inconsistent Indentation: Accidentally using 3 spaces on one line and 4 on the next causes an IndentationError. Always use multiples of 4.
  • Missing Colon: Forgetting the : after if, for, or def prevents the interpreter from recognizing the start of a block.
  • Unintended Dedenting: Placing a line outside a loop or function by mistake changes the logic. Check alignment carefully.

When to use it

You always use indentation in Python; there is no alternative syntax for defining blocks. However, understanding how it compares to brace-based languages helps when switching contexts.

Feature Python (Indentation) C/Java (Braces)
Block Definition Whitespace Curly Braces {}
Visual Clarity High (structure is visible) Medium (depends on formatting)
Error Type IndentationError Syntax Error / Logic Bug

Practice

Guided Exercise: Write a script that checks if a number is even. If it is, print "Even". Otherwise, print "Odd". Ensure proper indentation.

Challenge: Create a nested loop that prints a multiplication table for numbers 1 through 3. Use correct indentation to show the relationship between the outer and inner loops.

Hint: The inner loop's body should be indented twice relative to the outer loop's header.

Quick check

Question: What happens if you forget to indent the line following an if statement?

Answer: Python raises an IndentationError because it expects a block of code to follow the colon.

Summary

Python relies on indentation rather than braces to define code blocks, enforcing clean and readable structure. Mastering consistent 4-space indentation is essential for writing valid Python programs and avoiding common syntax errors.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python Indentation – FAQs

Quick answers about learning Python Indentation in Python.

This free note from Coding Now Tech Institute explains Python Indentation in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Python Indentation, is 100% free with no signup required.
With focused practice, most students grasp Python Indentation in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now