Indentation is nothing but adding whitespaces before the statement when it is needed. Without indentation Python doesn't know which statement to be executed to next. Indentation also defines which statements belong to which block. If there is no indentation or improper indentation, it will display "IndentationError" and interrupt our code.
Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced. The number of spaces in the indentation is variable, but all statements wiathin the block must be indented the same amount.
A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line.
For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block.
Python's default indentation spaces are four spaces. The number of spaces, however, is entirely up to the user. However, a minimum of one space is required to indent a statement. Indentation is not permitted on the first line of Python code.
Generally, four whitespaces are used for indentation and are preferred over tabs
for i in range(1,11):
print(i)
if i == 5:
break
num = [1, 2, 3, 4, 5, 6, 7]
for i in num:
print(i)
if i==3:
break
print("End of for loop")
Indentation can be ignored in line continuation, but it's always a good idea to indent. It makes the code more readable. For example: