Relate to Python Indentation and write your Python blocks in less than 4 minutes.
Pick between 157 and 17. What is that supposed to mean? Can't understand? Then keep reading to make sense of it.
Indentation is all about the space before each line. Does that seem complicated or confusing? Then, this is the perfect read for you.
Check the two examples below, and try guessing the outputs. Remember to consider them as two separate snippets.
print(1,end='')
if 3+1>0:
print(2,end='')
elif 4-1>3:
print(3,end='')
elif 2>4:
print(4,end='')
else:
if 4>0:
print(5,end='')
else:
print(6,end='')
print(7)
print(1,end='')
if 3+1<0:
print(2,end='')
if 4-1>3:
print(3,end='')
elif 2>4:
print(4,end='')
else:
if 4>0:
print(5,end='')
else:
print(6,end='')
print(7)
Can't figure it out? Keep reading, and by the end, you should be able to answer. But if you do have a guess, scroll to the end for the answer.
print("HELLO")
The above line of code should print "HELLO," which is easy to figure out. One advantage of python is its readability.
print(1,end='')
print(2,end='')
if 3-1==2:
print(3,end='')
print(4,end='')
Imagine this code flow like this -
Try the one below -
print(1,end='')
print(2,end='')
if 3-1==2:
print(3,end='')
else:
print(5,end='')
print(4,end='')
Look at the flow below and trace it out for more clarity.
Yes, the previous 2 code blocks give the same output. It's 1234.
So when you choose YES, you would never know you check what is with NO. The same if for the if-else or if-else ladder.
print(1,end='')
print(2,end='')
if 3-1==2:
print(3,end='')
elif 6+9>0:
print(6,end='')
else:
print(5,end='')
print(4,end='')
The flow would be as below —
Indentation is about spaces by which you need to understand the statement falls under what.
Thanks for reading! Have a nice day, and do follow me for more such stories! Yes, I didn't forget, but my friend, you had already read through the answer and didn't realize it.
(Hint: the answer is mentioned in the article, did you find it?)