Learn Python Indentation
In Python, indentation refers to the use of whitespace (spaces or tabs) to denote block-level structure in the code. Python uses indentation to define the scope of code blocks, such as:
In Python, indentation is mandatory and is used to determine the grouping of statements. The number of spaces used for indentation is not fixed, but it’s standard to use 4 spaces for each level of indentation. Read more: Indentation - PEP 8 – Style Guide for Python Code
Here’s an example:
if True:
print("Hello") # This line is indented with 4 spaces
print("World") # This line is also indented with 4 spaces
In this example, the two print statements are indented with 4 spaces, indicating that they are part of the if block.
Python’s indentation system has several benefits, including:
Another example, consider the following code snippet:
if True:
print("True")
else:
print("False")
Task: 15 Please correct the following Python code:
if True:
print("True")
print("False")
Error message: IndentationError: unexpected indent
”
See also: