Python Comments - GeeksforGeeks

11 Apr, 2022
Comments in Python are the lines in the code that are ignored by the interpreter during the execution of the program. Comments enhance the readability of the code and help the programmers to understand the code very carefully. There are three types of comments in Python – 
Single line Comments
Multiline Comments
Docstring Comments
Python3
# Python program to demonstrate comments # sample commentname = "geeksforgeeks"print(name)
Output: 
geeksforgeeks
In the above example, it can be seen that comments are ignored by the interpreter during the execution of the program.
Comments are generally used for the following purposes: 
Code Readability
Explanation of the code or Metadata of the project
Prevent execution of code
To include resources
There are three main kinds of comments in Python. They are: 
Python single-line comment starts with the hashtag symbol (#) with no white spaces and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment. Python’s single-line comments are proved useful for supplying short explanations for variables, function declarations, and expressions. See the following code snippet demonstrating single line comment:
Example: 
Python3
# Print “GeeksforGeeks !” to consoleprint("GeeksforGeeks")
GeeksforGeeks
Python does not provide the option for multiline comments. However, there are different ways through which we can write multiline comments.
We can multiple hashtags (#) to write multiline comments in Python. Each and every line will be considered as a single-line comment.
Python3
# Python program to demonstrate# multiline commentsprint("Multiline comments")
Multiline comments
Python ignores the string literals that are not assigned to a variable so we can use these string literals as a comment. 
Python3
'This will be ignored by Python'
On executing the above code we can see that there will not be any output so we use the strings with triple quotes(“””) as multiline comments.
Python3
""" Python program to demonstrate multiline comments"""print("Multiline comments")
Multiline comments
Python docstring is the string literals with triple quotes that are appeared right after the function. It is used to associate documentation that has been written with Python modules, functions, classes, and methods. It is added right below the functions, modules, or classes to describe what they do. In Python, the docstring is then made available via the __doc__ attribute.
Example:
Python3
def multiply(a, b):    """Multiplies the value of a and b"""    return a*b  # Print the docstring of multiply functionprint(multiply.__doc__)
Output: 
Multiplies the value of a and b
nikhilaggarwal3
yahiadjem
python-basics
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python