--Originally published at Quirino´s Projects
Comments come very useful when it comes to writing your program, it helps you to do organized code and more easy to understand, for you, or who ever needs to evaluate your code, for example instead of naming a function.
Commentaries do not affect your code
def a_function_that_adds_two_numbers_and_returns_its_double(x,y): pass
You could just do
#This function adds two numbers and returns its double def add_double(x,y): pass
Even tho you know what your codes do, sometimes is useful to let others understand easier and faster what you are trying to to, or you just could forget what the function/method meant like me
Another useful tip for python are multi-line comments using
''' love sheep. So do I. Terrific animals. Terrific. trouble. No, no trouble. Except at shearing. They can play up a bit, then; can't they?'''
Unlike single line comments #, you need to close the ”’ or you will have problems
Like this:
''' This program is useful when you want to add two numbers. It reads from the user 2 input and returns its sum def add(): x = input("Please Give me a number: ") y = input("Input another one: ") return ("The sum of %s and %s is %s") % (x,y,x+y) print add()
This would run generate the following error:
File “Mastery_Topics_Comments.py”, line 10
SyntaxError: EOF while scanning triple-quoted string literal
This would be the right way: (You could copy this code but i don’t know how to indent in wordpress editor) delete [TAB] and tab it yourself
''' This program is useful when you want to add two numbers. It reads from the user 2 input and returns its sum''' def add(): [TAB]x = input("Please Give me a number: ") [TAB]y = input("Input another one: ") [TAB]return ("The sum of %s and %s is %s") % print add()
Sometimes you will feel commenting is not that useful, but when it comes to big programs it really is