Abraiz Ali Khan Naqshbandi
PRO
last week
Abraizcountry asked

I am confused with the indentations. All my errors have been of indentations. Some help in this regard?

N
Expert
yesterday
Nisha Sharma answered

Hello there!

Indentation is a common pain point in Python because it’s not just style, it’s part of the syntax. A few rules will save you a lot of errors:

  • For blocks that end with : (like if, for, while, def), the very next line must be indented.

  • Everything inside that block must line up at the same indentation level.

  • Nested blocks go one level deeper (usually 4 spaces).

  • Don’t mix tabs and spaces. Pick one (4 spaces is the standard) and stick to it.

Example:

for i in range(1, n + 1):      # level 0
    if i % 2 == 0:             # level 1
        continue               # level 2 (inside the if)
    print(i)                   # level 1 (outside the if)

If you keep getting errors, it’s usually one line that’s slightly misaligned or tabs are sneaking in. Feel free to reach out if you have any more queries.

Python
This question was asked as part of the Learn Python Basics course.