Comments in Python are lines of code that are ignored by the interpreter. Python supports single-line and multi-line comments. A single-line comment makes the interpreter skip one line of code while a multiline comment can disable more than one line from execution.
Why comments are important in Python?
Writing comments is a good programming practice. They are non-executable parts of the code, yet quite essential in a program. These not only help other programmers working on the same project but the testers can also refer them during white-box testing.
Must Read: How do you comment on multiple lines in Python?
What is the purpose of comments?
Comments in Python are non-executable lines of text for providing explanations, notes, or annotations within the code. They are skipped from execution by the Python interpreter. However, they are displayed in the interpreter’s output so that you can still see them.
A comment can be used for several purposes which are as follows:
How to write comments in Python?
There are two ways to write comments in Python:
Single-line comments
Single-line comments start with a hash symbol (#
) and continue to the end of the line.
# Calculate the area of a rectangle area = length * width # Increment the count of wizards by one wizards += 1 # Magical! # TODO: Implement the magic_sort function to sort the elements in ascending order def magic_sort(elements): # ... magical implementation ...
Also Check: Python multiline string with variables
Python multiline comment
Multi-line comments start with three double quotes ("""
) or three single quotes ('''
) and end with the same punctuation.
''' This is a multi-line comment. It can span across multiple lines. ''' def add_numbers(a, b): """ This is another multiline comment. It can also span across multiple lines. """ return a + b
Both triple single quotes and triple double quotes are equivalent and can be used interchangeably to create multiline comments.
While you are here, let us share one of our most followed resources, 100 Python interview questions. Whether you are preparing for a job interview or want to put your skills to the test, we highly recommend checking them out.
Python comment block
A Python comment block, also known as a multiline comment or docstring, is a section of code that is ignored by the Python interpreter. It is used to explain the code to other programmers, or to yourself so that you can understand what the code is doing.
In Python, a comment block can be created using three double quotes ("""
) or three single quotes ('''
). The comment block must start and end with the same punctuation, and the text between the punctuation is ignored by the interpreter.
Multiline comment, docstring, and comment block are interchangeable terms.
For example, the following code creates a comment block:
"""This is a comment block.
It can be used to explain the code or to prevent the interpreter from executing certain lines of code."""
Comment blocks are a great way to explain what your code is doing. They can also be used to prevent the interpreter from executing certain lines of code. For example, if you are debugging your code, you can use comments to temporarily disable certain lines of code.
Examples: Comments in Python
Here are some unique, practical, and innovative examples of comments in Python:
Using comments to explain the purpose of a function
The following code defines a function called calculate_area()
. The comment block explains what the function does, and how to use it.
def calculate_area(width, height): """ This function calculates the area of a rectangle. Args: width: The width of the rectangle. height: The height of the rectangle. Returns: The area of the rectangle. """ area = width * height return area
Using comments to document the code
The following code defines a class called Circle
. The comment block explains what the class does, and what its attributes and methods are.
class Circle: """ This class represents a circle. Attributes: radius: The radius of the circle. Methods: __init__(self, radius): Initializes the circle. calc_area(): Calculates the area of the circle. calc_circumference(): Calculates the circumference of the circle. """ def __init__(self, radius): self.radius = radius def calc_area(self): area = math.pi * self.radius ** 2 return area def calc_circumference(self): circumference = 2 * math.pi * self.radius return circumference
Using comments to prevent execution
The following code defines a function called debug()
. The function is commented out, so it will not be executed. This can be useful for debugging code, or for temporarily disabling code that you are not currently using.
# def debug(): # print("This is a debug function.") # debug()
We hope these examples give you some ideas for using comments in your own Python code. Furthermore, we have another tutorial on how to comment out multiple lines of code in R programming. You may like to check out if you are enthusiastic about this language.
Before You Leave
Comments, comment blocks, and docstrings add value to a program. They make your programs more readable and maintainable. Even if you need to refactor the same code later, then it would be easier to do with the comments available.
Software spends only 10% time of its life in development and rest of 90% in maintenance.
Hence, always put relevant and useful comments or docstrings as they lead to more collaboration and speed up the code refactoring activities.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Facebook) if you gained some knowledge from this tutorial.
Enjoy coding,
TechBeamers.