Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Comments

  • Last Updated : 18 Aug, 2021

Comments in Python are the lines in the code that are ignored by the compiler 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

Example: Comments in Python

Python3




# 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 in Python

There are three main kinds of comments in Python. They are: 



Single-Line Comments

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 console
print("GeeksforGeeks")
Output
GeeksforGeeks

Multi-Line Comments

Python does not provide the option for multiline comments. However, there are different ways through which we can write multiline comments.

Using Multiple Hashtags (#)

We can multiple hashtags (#) to write multiline comments in Python. Each and every line will be considered as a single line comment.

Example: Multiline comments using multiple hashtags (#)

Python3




# Python program to demonstrate
# multiline comments
print("Multiline comments")
Output
Multiline comments

Using String Literals

Python ignores the string literals that are not assigned to a variable so we can use these string literals as a comment

Example 1:

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.

Example 2: Multiline comments using string literals

Python3




""" Python program to demonstrate
 multiline comments"""
print("Multiline comments")
Output
Multiline comments

Python Docstring

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 function
print(multiply.__doc__)

Output: 

Multiplies the value of a and b




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!