+ 1

What's Indentation and how is it used with Python?

5th May 2017, 5:50 PM
Larry Kay
Larry Kay - avatar
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 {}.
5th May 2017, 5:57 PM
Tob
Tob - avatar
+ 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 :)
5th May 2017, 6:45 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 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
5th May 2017, 5:59 PM
Nilesh
Nilesh - avatar
+ 1
practical advise: expert says, dont mix spaces and tabs as intendation, it can cause troubles.
5th May 2017, 7:20 PM
Benedek Måté Tóth
Benedek Måté Tóth - avatar