Python Comment


Python Introduction By CodeHind Image

Comment

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 Comment
  • Multi-Line Comment


Example: Comment In Python


#Python Program to Understand Comments
name="CodeHind"
print(name)

=======================================

Output:
CodeHind

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


Why We Use Comments ?

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 Two main kinds of comments in Python.

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 :

#Print “CodeHind” to Console
print(“CodeHind”)

=====================================

Output :
CodeHind

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 (#)

# 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 :


'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

""" 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 :

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


How to Write Better Comments ?

  • Use comments to describe what a function does and not the specific details on how the function does it.
  • Try to remove as many redundant comments as possible. Try writing code that can explain itself, using better function/variable name choice.
  • Try to make the comments as short and concise as possible.