In this tutorial you will learn about documenting and commenting on the python code using python comments and python docstring.
What Is Comment In Python?
It is tough to remember every variable’s name, function name, and class name when we have a hundred-page program or so. Therefore, making use of comments will make it very easy for you or someone else to read and modify the code.
How to comment in python
Python comments syntax:
In python we can # for single line comment and ”’ or “”” for multiline comments and this are the only two syntax for python comment.
TYPES OF COMMENTS IN PYTHON
There are two types of comments Single-line comments and multi-line comments, which are available in python, or we can say which are available in every programming language.
Single Line Python Comment
A single line comment is used by adding a hash sing (#) at the beginning of a text or a string statement.
# This is a single-line comment
print("This is A String") # This is Comment
This is A String
As we can see only, the print function is executed, and the comment was not printed on the output. We can add comments in the code, but this is not the only method to add single-line comments. We can also add comments after our function or end of the code.
#Heading print("String start from here") #And Ends here #Closeing comment
Staring start from here
Multiline Comment In Python
What Is Multiline Comments In Python?
Multiline comments in python are the comments thats includes commenting multiple lines of code in one.
We can create a python multiline comment by adding a delimiter (“”) on the starting and end of the statement.
""" This is a multiline comment. Using python, and it will not be printed. At the runtime """
print("Hello World") """ This is a multiline comment Using python, and it will not be printed. """
Multiple Python Comments
What Is Python Multiple Line Comments?
In python we can add multiple line comments using three “”” inside the code, here the python interpreter will ignore the code or text between three quotes.
We can add multiple comments in a single program or code.
Here is the example of python multiple line comment.
# This is Header """ We are going to Print hello world """ print("Hello World") # This is a print function """ Here we will print Other print function """ print("It is Python comment") # Print function to print It is python comment # This is Footler
WHY COMMENTS ARE USED IN PYTHON.
Comments in Python are used to explain code and what it does. They are meant as documentation for anyone reading the code.
Python docstrings or Documentation strings are a string used in the class, module, function, or method definition.
As like multiline comment, docstring is also declared using three (”’) or four (“””). For example ”’ triple single quotes ”’ or “”” triple double quotes “”” Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and built-in functions. Docstrings are great for understanding the functionality of the more extensive code of the project.
Example of Code:
def addition(n): ''' This is a docstrings example we have added in addition function''' return n+n print(addition.__doc__)
To run this docstring code, we have to follow this step.
Here the output of string literal.
This is a docstrings example we have added in the addition function.
Here, we have documented our addition function, and then we are accessing it with the __doc__ attribute.
We can learn more about python docstring from here.
- Remember to comment as often as possible.
- Mainly used single-line comment after the code, so it will be easy to read code for others.
- Comments don’t mess up with the code, so it is essential to add as many comments as possible for documentation.
- The first includes comments that detail or indicate what a section of code – or snippet – does.
- Think of the first type as a comment for yourself and the second as a comment for others.
- Always go through the official python comments documentation
How to make Comment in python?
In python we can use # hashtag special characters to make comments in python in addition to that we can use triple single quotes or triple double quotes to make multiple line comments.
How to print comments from a program in python?
Unfortunately, comments are developed in such a way that all the programming languages will ignore them.
In Python, we can’t print any comments using any program or comments.
What is a python comments shortcut?
In most IDE’s like Pycharm, VS code, Jupyter Notebook, etc we can use “Ctrl + / ” for inline comments or for multiple line comments
What are the types of comments in python?
There are only two types of python comments.
1) Single line or inline comments
2) Multiline comments