Python Indentation Rules
Python block scope can be created by white space or tabs.
To create a new block scope use the same number of them consistently.
i = 1 while (i <= 3): ..print(i) ..i = i + 1
Output:
1 2 3
Any number of spaces will work as long as it's consistent.
def fun(): ....print("statement 1") ....print("statement 2") ....print("statement 3")
Run the function:
fun()
Output:
statement 1statement 2
statement 3
Tabs or Spaces?
It is generally suggested to use spaces. But tabs are preference of many and can be used with same degree of success. What's important is to remain consistent. For that reason in Python 3 you can't mix spaces and tabs.
When choosing Python indentation rules you're going to follow when writing your program just make sure to keep the same number of spaces within same scope.
Incorrect indentation in Python will produce TabError error
The following is an example of improper indentation that will result in an error:
def fun(): ..print("statement 1") ...print("statement 2")
The second line in function body has inconsistent number of spaces to the first line.
TabError: inconsistent use of tabs and spaces in indentation