Comments in Python are used to explain the code and make it easier to understand. They help developers read and maintain the program.
In this chapter, you will learn about Python comments, their types, and how to use them in your code.
In Python, comments are lines in the code that are ignored by the interpreter during execution. They are written to explain the code, improve readability, and help in debugging and documentation.
In the following example, a comment is used to explain the code, and the print() function displays the output.
Output:
Welcome to Tpoint Tech to learn Python
There are primarily three types of comments used in Python, such as:

Let us discuss these types with the help of examples:
In Python, a single-line comment starts with a hash '#' symbol, and Python will ignore it. The single-line comments are utilized to provide short explanations or notes about the code.
Let us see a simple example of single-line comments in Python.
Output:
Welcome to Tpoint Tech!
Explanation:
In the above example, we have added a single-line comment using the hash '#' symbol. As a result, the Python interpreter ignored this line of code and executed the next line of code to print a statement.
An inline comment is a type of single-line comment that appears on the same line as a statement and is used to explain a particular segment of that line.
Here is an example of inline comments in Python.
Output:
Welcome to Tpoint Tech!
Explanation:
In the above example, we have added an inline comment using the hash '#' symbol after the print() function. Python interpreter has ignored the commented segment from the line of code.
Unlike other programming languages like C, C++, and Java, Python does not have a dedicated way of writing multi-line comments.
However, a similar effect can be achieved using the following ways:
One of the basic ways of adding multi-line comments to the source code is by stacking single-line comments with the help of hash symbols '#' on each line.
Take a look at the following example to write multi-line comments using multiple hash symbols:
Output:
Welcome to Tpoint Tech!
Explanation:
Here, the multiple single line comments are stacked together to make it look like a multi-line comment.
We can simulate multi-line comments in Python, with the help of triple quoted strings (''' or """). Even though they are technically multi-line strings, we can use them as comments.
Here is a simple example showing the way of adding multi-line comments to the code using the triple-quoted strings.
Output:
Welcome to Tpoint Tech!
Explanation:
Although triple quotes are not technically comments, but in the above example, we have used it as a comment for quick notes or debugging.
Docstrings, short for documentation strings, are special multi-line strings in Python that we can utilized in order to document functions, classes, methods, and modules.
Unlike regular comments, docstrings are stored at runtime. We can access them using the built-in help() function or the __doc__ attribute. This makes them incredibly helpful in building a well-documented, maintainable code.
Here's a quick example to understand the working of docstrings in Python.
Output:
Hello, John!, Welcome to Tpoint Tech. Docstring: This function prints a welcome message for the user
Explanation:
Here, the docstring is stored in the memory during the runtime. And when we called the __doc__ attribute, the stored docstring is returned.
We request you to subscribe our newsletter for upcoming updates.