0
1.2kviews
Explain different types of comments in python.
1 Answer
0
81views

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:

enter image description here

Output:

enter image description here

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.

enter image description here

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.

enter image description here

Using String Literals to write Multi-line Comments:

enter image description here

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.

Please log in to add an answer.