+ 1
During if and else statements,what are the rules of indentation?
During if and else statements,i didn't understand the indentation that how much spaces should i leave.is there any rule for it??
5 Respostas
+ 5
Indentations are used to denote a block of code. Basically, every time you end a line with a colon (:) the following line needs to be indented:
if condition:
print('yes')
print('no')
Here, both lines after the if statement are indented so they are both executed if "condition" is True.
if condition:
print('yes')
print('no')
Here, only the first line after the if statement is indented, so "yes" will only be printed if "condition" is true. The last line is not indented, so it will be printed no matter if "condition" is true or not. Because there is no indentation, the last line does not belong to the block of code that follows the if statement, but forms a new block of code.
An else statement has to be on the same indentation level as the corresponding if statement:
if condition:
print('yes')
else:
print('no')
print('ok')
print('hm')
If "condition" is true, "yes" is printed. If it is false, "no" and "ok" are printed. The last line isn't indented so it doesn't belong to the if/else statement and "hm" gets printed in either case.
+ 3
For python, people use mostly 4.
+ 1
It's a sample from the Sololearn course:
https://code.sololearn.com/cnQL5DracWPZ/?ref=app
+ 1
https://code.sololearn.com/c9a0cG9dpVUr/?ref=app
0
Please give an example in phython