Python Tips — How Indentation Works

Indentations rules you may not know in Python

Tony
Geek Culture

--

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. Without proper indenting the Python code, you will end up seeing IndentationError and the code will not get compiled.

This article shows you a few tips about Python indentation which you may never pay attention.

For example, if the indentation does not conform to the rules, the parser will report an indentation error and the program cannot run:

Depending on the indentation, the effect of program execution may also be different.

For example, in the code in the figure, the code on the left will print the output “Print anyway”, while the code on the right will not at all. Because the indentation is different.

--

--