Indentation Aesthetics: Python’s Poetic Expression of Clean Code

Mahalakshmi Jayaprakasan
3 min readJul 31, 2023
Indentation levels of python

Python Indentation :

Indentation refers to the spaces at the beginning of a code line.

In every other programming languages the indentation is significant for code readability . In Python, indentation is not just for aesthetics but is syntactically significant and plays a fundamental role in the structure .Also readability counts.

It is used to define code blocks, such as loops, functions, classes, and conditionals. Each level of indentation represents a new block of code.

Indentation level:

The most common indentation style in Python is using four spaces for each level of indentation. This is recommended by the official Python style guide (PEP 8) and is widely adopted by the Python community.However, the number of spaces can be anything; it is up to the user. But a minimum of one space is needed to indent a statement. The first line of python code cannot have an indentation.

# Example of Python indentation
def example_function():
if some_condition:
print("Condition is true.")
else:
print("Condition is false.")

for i in range(5):
print(i)

# Function call
example_function()

In this example, the indentation level helps Python understand which statements belong to the example_function() and the if-else block and the for loop.

Consistency:

It’s important to be consistent with indentation throughout the codebase. All statements within the same block must be indented at the same level.

Spaces or Tabs:

Mixing tabs and spaces for indentation is discouraged as it can lead to syntax errors.However, it’s recommended to use spaces for better consistency, as tabs may be interpreted differently in different environments.

Wrong Indentation(Error):

Example 1:

# Wrong indentation followed in last line
1 if( 1 == 1):
2 print("This is test code")
3 print("This is test code1")

Python will throw “IndentationError: unindent does not match any outer indentation level”.

Example 2:

# Wrong indentation followed in last line
1 if( 1 == 1):
2 print("This is test code")
3 print("This is test code1")

Python will throw “IndentationError: expected an indented block error”.

Example 3:

# Wrong indentation followed in last line
1 if( 1 == 1):
2 print("This is test code")

Python will throw “IndentationError: expected an indented block error”.

With Correct Indentation:

# four spaces of indent is used here to define if block
if( 1 == 1):
print("This is test code")
print("This is test code1")

or

# four spaces of indent is used here to define if block .
# last line with no indent
if( 1 == 1):
print("This is test code")
print("This is test code1")

Conclusion :

Remember that indentation is not just about aesthetics; it’s a key part of Python’s syntax, and incorrect indentation can lead to errors and unexpected behaviour. So, always maintain proper and consistent indentation to create readable and functional Python code.

“Strategic spaces enhance the Code Structure ; use them judiciously, where needed, like stars amidst the night sky….”

--

--

Mahalakshmi Jayaprakasan

A Young women who is passionate on learning and exploring technologies..