Open In App

Python Comments: Importance, Types and Correct Way to Use

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Comments in Python are the lines in the code that are ignored by the interpreter during the execution of the program.




# I am single line comment
  
  
""" Multi-line comment used
print("Python Comments") """


Comments enhance the readability of the code and help the programmers to understand the code very carefully. It also helps in collaborating with other developers as adding comments makes it easier to explain the code.

Types of Comments in Python

There are three types of comments in Python:

  • Single line Comments
  • Multiline Comments
  • String Literals
  • Docstring Comments

Comments in Python

In the example, it can be seen that Python Comments are ignored by the interpreter during the execution of the program.

Python3




# sample comment
name = "geeksforgeeks"
print(name)


Output: 

geeksforgeeks

Why are Comments Used in Python?

Comments have been an integral part of programming languages, and every language have different ways of using comments.

Just like any other language, comments in Python serve following purpose:

  1. Enhance code readability
  2. Explaining code to other
  3. Understanding code if studied after some time
  4. Documenting the steps and needs for a function
  5. Sharing code with fellow developers
  6. Collaborating with multiple people.

Types of Comments in Python

Let’s discover the different types of comments, how to use them and when to use them?

1. 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 Python 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




2. Multi-Line Comments

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

a) 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.

Python3




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


Output

Multiline comments




Also Check: Interesting Fact about Python Multi-line Comments

3. String Literals

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

a) Single-line comments using string literals

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




'This will be ignored by Python'


b) Multiline comments using string literals

Python3




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


Output

Multiline comments




4. 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

Advantages of Comments in Python

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

Right Way to Write Comments

Comments serve the main purpose to explain your code. Developers use healthy comment writing practice for better understanding of the code.

Some of the tips you can follow, to make your comments effective are:

  1. Comments should be short and precise.
  2. Use comments only when necessary, don’t clutter your code with comments.
  3. Comment should have some meaning.
  4. Avoid writing generic or basic comments.
  5. Write comments that are self explanatory.

We have discussed all about Python comments, how to write Python comments, types of comment,what are it’s advantages and the right way to write comments.

Also Read: How to write Comments in Python3?



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads