Python Comment is a fundamental tool for the software engineers. Comments are generally utilized to explain the code. We can easily get the code if it has a proper clarification. A good developer should utilize the comments because in the future anyone wants to modify the code as well as implement the new module; then, it can be done without any problem.
There are mainly three types of comments in Python programming –
- Single line Comments
- Multiline Comments
- Docstring Comments
Comments are generally utilized for the accompanying purposes:
- Code Readability
- Clarification of the code or Metadata of the project
- Prevent execution of code
- To include resources
Types of Comments in Python Language with Example
There are three main types of comments in Python. They are:
Single-Line Comments in Python
Python single line comment begins with the hashtag symbol (#) with no blank areas and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the following line and proceed the comment. Python’s single-line comments are proved valuable for providing short clarifications for variables, function declarations, and expressions. See the accompanying code snippet showing single line comment:
Example:
# printing a string
print('Hello world')
Output:-
Multi-Line Comments in Python
Python doesn't give the choice for multiline comments. However, there are various ways through which we can compose multiline comments.
We can multiple hashtags (#) to compose multiline comments in Python. Every line will be considered as a single line comment.
Example: Multiline comments utilizing multiple hashtags(#)
# Python program to demonstrate # multiline comments print("Multiline comments")
Output:-
Python ignores the string literals that aren't assigned to a variable so we can utilize these string literals as a remark.
Example 1:
'This will be ignored by Python'
On executing the above code we can see that there will not be any output so we utilize the strings with triple quotes(“””) as multiline comments.
Example 2: Multiline comments using string literals
""" Python program to demonstrate multiline comments""" print("Multiline comments")
Output:-
Python Docstring Comments
Python docstring is the string literals with triple quotes that are showed up just after the function. It is utilized to associate documentation that has been composed with Python modules, functions, classes, and methods. It is added right beneath the functions, modules, or classes to depict what they do. In Python, the docstring is then made accessible through the __doc__ attribute.
Example:
def multiply(a, b): """Multiplies the value of a and b""" return a*b # Print the docstring of multiply function print(multiply.__doc__)
Output:
0 Comments
🙏🙏please don't enter any spam links on the comment box.....