Python Tutorial
Python OOPs
Python MySQL
Python MongoDB
Python SQLite
Python Questions
Python Tkinter (GUI)
Python Web Blocker
Python MCQ
Python Comments
Python Comments
We'll study how to write comments in our program in this article. We'll also learn about single-line comments, multi-line comments, documentation strings, and other Python comments.
Introduction to Python Comments
We may wish to describe the code we develop. We might wish to take notes of why a section of script functions, for instance. We leverage the remarks to accomplish this. Formulas, procedures, and sophisticated business logic are typically explained with comments. The Python interpreter overlooks the remarks and solely interprets the script when running a program. Single-line comments, multi-line comments, and documentation strings are the 3 types of comments in Python.
Advantages of Using Comments
Our code is more comprehensible when we use comments in it. It assists us in recalling why specific sections of code were created by making the program more understandable.
Aside from that, we can leverage comments to overlook specific code while evaluating other code sections. This simple technique stops some lines from running or creates a fast pseudo-code for the program.
Below are some of the most common uses for comments:
- Readability of the Code
- Restrict code execution
- Provide an overview of the program or project metadata
- To add resources to the code
Types of Comments in Python
In Python, there are 3 types of comments. They are described below:
Single-Line Comments
Single-line remarks in Python have shown to be effective for providing quick descriptions for parameters, function definitions, and expressions. A single-line comment of Python is the one that has a hashtag # at the beginning of it and continues until the finish of the line. If the comment continues to the next line, add a hashtag to the subsequent line and resume the conversation. Consider the accompanying code snippet, which shows how to use a single line comment:
Code
Output:
This statement does not have a hashtag before it
The following is the comment:
The Python compiler ignores this line.
Everything following the # is omitted. As a result, we may put the program mentioned above in one line as follows:
Code
Output:
This is not a comment
This program's output will be identical to the example above. The computer overlooks all content following #.
Multi-Line Comments
Python does not provide the facility for multi-line comments. However, there are indeed many ways to create multi-line comments.
With Multiple Hashtags (#)
In Python, we may use hashtags (#) multiple times to construct multiple lines of comments. Every line with a (#) before it will be regarded as a single-line comment.
Code
- # it is a
- # comment
- # extending to multiple lines
In this case, each line is considered a comment, and they are all omitted.
Using String Literals
Because Python overlooks string expressions that aren't allocated to a variable, we can utilize them as comments.
Code
- 'it is a comment extending to multiple lines'
We can observe that on running this code, there will be no output; thus, we utilize the strings inside triple quotes(""") as multi-line comments.
Python Docstring
The strings enclosed in triple quotes that come immediately after the defined function are called Python docstring. It's designed to link documentation developed for Python modules, methods, classes, and functions together. It's placed just beneath the function, module, or class to explain what they perform. The docstring is then readily accessible in Python using the __doc__ attribute.
Code
Output:
This function adds the values of x and y