Definition and Type of Python Comment
Categories: PHP
Python Comment is a programmer’s tool. We use them to explain the code, and the interpreter ignores them.
You never know when a programmer may have to understand code written by another and make changes to it.
Other times, give your brain a month’s time, and it might forget what it once had conjured up in your code.
For these purposes, good code will hold comments in the right places.
1. Multiline Python Comment
To have multiline python comment in your code, you must use a hash at the beginning of every line of your comment in python.
>>> #Line 1 of comment
>>> #Line 2 of comment
>>> #Line 3 of comment
This comment\nis spanned across\nmultiple lines’
This gives us an output because we type it in the shell. When you create a file and write this in that, there is no output.
While triple quotes are generally used for multiline python comment, we can conveniently use them for python comment as well.
Triple quotes will also preserve formatting.
2. Docstrings Python Comment
A docstring is a documentation string in Python. It is the first statement in a module, function, class, or method in Python.
In this, you will learn what a python function/class does.
>>> def sayhi():
This function prints Hi
print("Hello")
>>> sayhello()
Output
Hello
To check a function’s docstring, use its __doc__ attribute.
>>> def sayhello():
This function prints Hello
print("Hello")
>>> sayhello.__doc__