0
Couldnt understand the utility of indentation in python. Can anybody elaborate plx?
6 ответов
+ 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.
+ 1
your code won't work
+ 1
I suggest u to learn C first if u are struggling with python. The concept of indentation would be very clear then.
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 ...)
0
what if we dont do indentation, then whats the penalty?
0
thanx alot sapphire