Back to: Python Programming
A comment is a part of a Python program that will not be executed and will be ignored by the interpreter or compiler when the program is run.
Comments are used to describe and document the code. An industry-level program may contain thousands of lines of code. Therefore, pointing out which part of the program does what is important. For that purpose, comments are used.
Types of comments in Python:
There are two types of comments in Python.
- Single-line comments and
- Multi Lines comment
For a single-line comment, a number or hash(#) is used. Let’s see an example:
#This line prints a greeting message print(“Hello everyone! Good morning!”)
Here there will be no impact of #This line prints a greeting message in the program’s output. This line will be ignored.
You can also add comments to the end of a line of code, like this:
print("Hello, world!") # This is a comment
In this case, the comment comes after the code, separated by a space, and is also preceded by the “#” character.
Multi lines comment:
Multi lines comment is for documenting or explaining the code in more detail. For making a multi-line comment, three quotation marks are used at the start and end of the commenting lines. Example:
""" This is a multi lines comments in Python. This commenting system is used for documenting the code """
We may think of making Multi lines comment using a single comment pattern, like:
#This is a multi lines comments in Python. #This commenting system is used for documenting the code
But this is not a good practice. Triple quotation marks pattern is the best way of making multi lines comments in Python.
It’s a good practice to add comments to your code, especially if you’re working on a large project or if you’re working with other developers. Comments can help explain how your code works and make it easier to understand.
However, be careful not to overdo it with comments. Too many comments can make your code harder to read, so it’s important to strike a balance between providing enough information and keeping your code clean and easy to read.