Python Comments: How, When, and Why You Should Use Them

Python Comments: How, When, and Why You Should Use Them

4 mins read1.2K Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Apr 30, 2024 09:36 IST

Comments in python are the line of codes that are ignored by python interpreter during the execution of code. In this article, we will briefly discuss how to comment in Python.

2022_11_MicrosoftTeams-image-72.jpg

When you write code, it is important to be easily understandable by others. To do that, you write the comments before or after declaring the variable, function, etc. It helps to document your codes. This article will discuss how to write comments in Python – its advantage and types.

So, let’s explore.

Table of Content

What are the comments in Python?

Comments in python are the line of codes that are ignored by the python interpreter during the execution of the program. It helps the developer to easily debug the codes.

  • Comments make the code more explainable and readable.
  • Comments prevent execution when testing code.

How to write good comments in Python?

There needs to be more than just writing comments; it should discuss the character of the defined variable and function. Here are some practices that you can follow to write feasible and efficient comments while writing codes:

  • Write comments only when necessary; otherwise, they may create clutter in your code block, leading to confusion.
  • It should be short and straightforward, i.e., easy to understand.
  • Use comments to describe what a function or variable is but not how it works.
    • Comments should always support the code.
  • Avoid writing harsh comments.
  • Avoid repeating the comments in a code block.

Types of Comments:

In Python, you can comment in three types: single-line comment, multiline comment, and docstring comment.

Single Line Comment in Python

It starts with # (hashtag symbol); anything written after that (till the end of the line) will be considered as a comment. It is helpful for defining the variables and function declaration.

Example: Print first 5 whole numbers.

 
#print first 5 whole number
number = range(5)
print(list(number))
Copy code

Output

Python Multiline Comment

Python does not support multiline comments, but there are different ways in which we can comment multiple lines in a code block. But technically, non of the ways are multiline comments.

Using Multiple #

If the comment exceeds more than a line, then again, put # at the start of the new line, and continue the comment. In this case, every line will be considered as a single-line comment.

Example : Define the whole number and print the first five whole numbers.

 
#Definition: All positive integers with zero is called Whole Number
#print first 10 whole number
number = range(5)
print(list(number))
Copy code

Output

Using String Literals

Python interpreters ignore the string literals that are not assigned as a variable so that string literals can be used as comments.

Example – 1

 
#This is a single line comment
'We are Shiksha Online'
print("Naukri learning")
Copy code

Output

We can use single or double quotes for the multiline comments in python. But in general practice, we use triple quotes.

Example -2

 
'''
We are Shiksha Online.
We have the best Best Online Courses and Certifications.
'''
print ("Shiksha Online")
Copy code

Output

Docstring Comment in Python

String literals that appear right after defining the function, method, class, or module is called Python DocString.
The docstring is available via the _doc_ attribute.

Example – 1:

 
#define the cube function
def cube(n):
"'Takes in a number n, returns the cube of n'"
return n**3
cube (2)
Copy code

Output

Example – 2:

 
#use _doc_ attribute
def cube(n):
'''Takes in a number n, returns the cube of n'''
return n**3
print(cube.__doc__)
Copy code

Output

Advantages of Using Comment in Python

  • It makes the code easy to understand and readable.
  • Prevent execution of code.
  • Blocking out specific codes.
    • It helps while testing the codes; you can comment on the codes that don’t need to be tested.
  • Comments make the codes self-explanatory.
    • It helps developers understand why certain variables or functions are used when they are tested after a long time.

Conclusion

In this article, we have briefly discussed how to comment in python with the help of examples.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

FAQs

What are comments in Python?

Comments in Python are lines in the code that are not executed. They are used to explain what the code does, making it easier to understand.

Do comments affect the execution of a Python program?

No, comments are ignored by the Python interpreter. They are meant for the person reading the code and do not influence program execution.

Why are comments important in Python?

Comments are crucial for documenting the code. They help others understand your code and assist you in remembering your own code's functionality when you return to it later.

Is there a standard for commenting in Python?

Python follows the PEP 8 style guide, which recommends clear, concise comments that reflect the code's intent. Comments should be complete sentences and should be used sparingly, i.e., only when necessary to explain complex parts of the code.

How do inline comments differ from regular comments?

Inline comments are placed on the same line as a statement, following the code. They are used to explain specific parts of a line. Regular comments typically appear on their own line.

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio