PRO
Abraiz
asked
N
Expert
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
:(likeif,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.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
