+ 5

Necessity of Indentation in python.

Why is indentation necessary in Python?

12th Jun 2019, 7:26 AM
Yash
Yash - avatar
5 Réponses
+ 9
Indentation marks a block of code, just like {} do in other languages. Other languages: if(condition) { print('inside block'); print('inside block'); print('inside block'); } print('outside block'); Python: if condition: print('inside block') print('inside block') print('inside block') print('outside block')
12th Jun 2019, 7:34 AM
Anna
Anna - avatar
+ 6
Because it doesn't use curly braces for example.
14th Jun 2019, 3:56 AM
Sonic
Sonic - avatar
+ 4
if True: print ("hi") print ("hello") if True: print("hi") print("hello")
12th Jun 2019, 7:29 AM
Cat Sauce
Cat Sauce - avatar
+ 4
Because we don't use curly braces for defining functions and classes if statements in python. So using indentation is the way we use to make compiler understand start or end of functions and classes and if statements .
12th Jun 2019, 7:30 AM
ΛM!N
ΛM!N - avatar
+ 3
otherwise the code cant tell what is in the if statement and what not
12th Jun 2019, 7:28 AM
Cat Sauce
Cat Sauce - avatar