written 2.1 years ago by |
Solution:
Comments are descriptions that help programmers better understand the intent and functionality of the program.
Comments are for developers. They describe parts of the code where necessary to facilitate the understanding of programmers, including yourself.
There are different types of comments used in python:
Single-line Comments.
Multi-line Comments.
Docstring Comments.
Python docstrings:
By convention, the triple quotes that appear right after the function, method, or the class definition is docstrings (documentation strings).
Docstrings are associated with objects and can be accessed using the “__doc__ “ attribute.
Input:
Output:
Single-line Comments:
Python ignores everything after the hash mark and up to the end of the line. You can insert them anywhere in your code, even in line with other code:
When you run the above code, you will only see the output This will run. Everything else is ignored.
Multiline Comments:
Python doesn't offer a separate way to write multiline comments. However, there are other ways to get around this issue.
We can use # at the beginning of each line of comment on multiple lines.
Using String Literals to write Multi-line Comments:
Here, the multiline string isn't assigned to any variable, so it is ignored by the interpreter. Even though it is not technically a multiline comment, it can be used as one.