Couldnt understand the utility of indentation in python. Can anybody elaborate plx? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Couldnt understand the utility of indentation in python. Can anybody elaborate plx?

21st Jun 2017, 11:27 AM
Omer mughale
6 Answers
+ 2
Indentation in python simply tells python where your statements/expressions/etc belong. This also makes readability and ease of use better for the programmer. You can easily bypass this with a semi colon ( ; ) and add multiple statements on the same line. But it makes your code harder to read and make changes to. Failing to indent properly will mostly give you angry red text, informing you that there is an indentation problem. Basically python doesn't know where the sudden indented block of code belongs. Expanding on what I said above. Indentation groups your blocks of code together. 1 1 1 _2 _2 __3 __3 This is most easily notiable on conditions. For example: x = 1 if x == 1: print (True) else: print (False) The print True function under the if condition is indented, indicating that the block of code belongs under the if statement. The print False is indented below the else, telling python that block of code belongs under it. If you aren't sure how the conditions above here work, sololearn explains this quite thoroughly, and my post cannot fit much more walls of text.
21st Jun 2017, 1:30 PM
Sapphire
+ 1
your code won't work
21st Jun 2017, 12:01 PM
clement
clement - avatar
+ 1
I suggest u to learn C first if u are struggling with python. The concept of indentation would be very clear then.
21st Jun 2017, 1:22 PM
Mohammad Fahad Sayed
Mohammad Fahad Sayed - avatar
0
The utility is that the python programs are easely readables and you don't have to always close your line (like ; in js or cpp or ...)
21st Jun 2017, 11:53 AM
clement
clement - avatar
0
what if we dont do indentation, then whats the penalty?
21st Jun 2017, 12:00 PM
Omer mughale
0
thanx alot sapphire
21st Jun 2017, 3:20 PM
Omer mughale