Comments are very important while writing a
program. It describes what's going on inside a program so that a person
looking at the source code does not have a hard time figuring it out.
You might forget the key details of the program you just wrote in a
month's time. So taking time to explain these concepts in form of
comments is always fruitful.
In Python, we use the hash (#) symbol to start writing a comment. It extends up to the newline character. Comments are for programmers for better understanding of a program. Python Interpreter ignores comment.
In Python, we use the hash (#) symbol to start writing a comment. It extends up to the newline character. Comments are for programmers for better understanding of a program. Python Interpreter ignores comment.
#This is a comment
#print out Hello
print('Hello')
Multi-line comments
If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the beginning of each line. For example:
#This is a long comment
#and it extends
#to multiple lines
Another way of doing this is to use triple quotes, either '''
or """
.
These triple quotes are generally used for multi-line strings. But they
can be used as multi-line comment as well. Unless they are not
docstrings, they do not generate any extra code.
"""This is also a
perfect example of
multi-line comments"""
Docstring
Docstring is short for documentation string. It is a string that occurs as the first statement in a module, function, class, or method definition. We must write what a function/class does in the docstring. Triple quotes are used while writing docstrings. For example:
def double(num):
"""Function to double the value"""
return 2*num
Docstring is available to us as the attribute __doc__
of the function.
>>> print(double.__doc__)
Function to double the value
No comments:
Post a Comment