Python developers often make use of the comment system as, without the use of it, things can get real confusing, real fast. Comments are the useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. Comments are usually helpful to someone maintaining or enhancing your code when you are no longer around to answer questions about it. These are often cited as a useful programming convention that does not take part in the output of the program but improves the readability of the whole program.
Example:
# Python program to demonstrate # comments # sample comment name ="geeksforgeeks"print(name) |
Output:
geeksforgeeks
In the above example it can be seen that comments are ignored by the compiler 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
Types of comments
There are three main kinds of comments in Python. They are:
- Single Line Comments: Python single line comment starts with 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:
# This is a comment# Print “GeeksforGeeks !” to consoleprint("GeeksforGeeks")chevron_rightfilter_none - Multi-Line Comments: Python multi-line comment is a piece of text enclosed in a delimiter
(""")on each end of the comment. Again there should be no white space between delimiter("""). They are useful when the comment text does not fit into one line; therefore needs to span across lines. Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet demonstrating multi-line comment:"""This would be a multiline comment in Python thatspans several lines and describes geeksforgeeks.A Computer Science portal for geeks. It containswell written, well thoughtand well-explained computer scienceand programming articles,quizzes and more.…"""print("GeeksForGeeks")chevron_rightfilter_none - Docstring Comments: Docstring is an in-built feature of Python, which 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 the Python, the docstring is then made available via the
__doc__attribute.Example:
defmultiply(a, b):"""Multiplies the value of a and b"""returna*b# Print the docstring of multiply functionprint(multiply.__doc__)chevron_rightfilter_noneOutput:
Multiplies the value of a and b
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Interesting Fact about Python Multi-line Comments
- Multiline comments in Python
- How do we create multiline comments in Python?
- How to write Comments in Python3?
- Django project to create a Comments System
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

