White space or indentation is extremely important in python

White space or indentation is extremely important in python. The reason is : there is no “{}” or any thing else that can define the block or space of the code in a text editor this white space or indentation is pre-formatted and comes by default but what you will do if you use ‘nano’ or ‘VIM’ or ‘gedit’ or ‘leafpad’ or ‘pyscript’ or anything that does not have it? You have to maintain a regular white space or indentation.

Look at this:


#!/usr/bin/python3
def main():
    print("This is main function.")
    print("This is another function.")

if __name__ == "__main__":
    main()

# here is the output
#
# This is main function.
# This is another function.
#

# now look at this code

def main():
    print("This is main function.")

print("This is another function.")

if __name__ == "__main__":
    main()

# the output changes
####################
# This is another function.
# This is main function.
####################
# it happens because python interpreter reads this first: print("This is another function.")
# and then call the main() function

# but this rule breaks only when there is one line of function like this

def main():print("This is main function.")

if __name__ == "__main__":main()

# there is no whitespace but it runs properly

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s