+ 1
What's Indentation and how is it used with Python?
4 Answers
+ 9
Indented text is placed farther to the right. In Python it it used to mark code blocks. Example:
if x==1:
print(2)
print("hi mom!")
x += 1
The print statements are indented, they form a block. This block is only executed if the condition is true, e.g. of x is one. The operation afterwards is not indented and will be executed whether x is 1 or not.
In many other languages code blocks are marked with curly braces {}.
+ 3
Just a remark - you pick the indentation step yourself - can be two spaces, can be four or whatever.
The convention is four, though. Don't change that unless you have a good excuse...
.
.
.
so probably never :)
+ 1
In python a number of statements are considered to be a single block if they have the same indentation. Similarly it comes under a loop or function if the block is sub-indented to it
+ 1
practical advise: expert says, dont mix spaces and tabs as intendation, it can cause troubles.