When we code in Python to make a program it becomes very important to write such code that other developers who will read our Python code could easily understand what is the specific line or block of Python code is supposed to do. It’s not only important that other developers can read your code but when you revisit your Python code after a long time you should also understand what you have coded then.
Giving variables a logical name and making your code more modular by defining Python functions, will make your code more readable. But by writing Python comments you can increase the readability of your Python code.
By the end of this Python tutorial article, you will have a complete idea on writing comments in Python. You will also learn, why it is important to write a comment in code, what are the different types of comments in Python and how to write Python comments to make the code cleaner and legible.
What are Comments in Python?
Python Comments are the text line in the program file which are not executed by the interpreter. The Python interpreter ignores the comment lines and moves further in search of the python code. The sole purpose of the comments to provide extra information about the code snippet.
The comment has nothing to do with the program execution and program result. The Python program will give the same result with or without comment unless you comment out some code lines.
To write a comment in Python we use the # symbol.
#this is a comment
This above example represents a single line comment, you can also use multi-line comments to comment out multiple lines. You will learn how to write proper single and multi-line comments in the next coming sections of this article.
Why it is Important to write comments in your code?
Programmer and Coders use comments in their programs to explain the working of modules, and functions as a docstring.
Comments play an important role in writing good code, the proper mentioning of comments helps the developer to grab a brief idea about the working of a module or code snippet.
Suppose you have worked on a project 6 months ago, and your boss wants you to put some updates on that project. By now you have no idea about the functioning of every module because the project contains thousands of modules and functions. Only if you have mentioned the proper comments in the program, you will be able to understand the overall working of each and every module. Else you have to go through the complete project to find out the working of the program.
Writing comments in the program is a good practice in programming, and it is one of the things which make your program a good code. A good coded program makes it easier for developers to understand what the program is all about and how it going to work. It is not necessary that a good code always give you a perfect output, but it helps you to debug the code easily.
If you write a code for a big project then other developers who will be working on the same project will also read your code. So, by mentioning comments for every module you write and every important call you make in your Python program make sure that you mention a comment.
How to write comments in Python?
Now we know what are comments in Python and why it is important to write comments in a program. Now let’s discuss how to write comments in Python.
There are 2 ways of how can we write a comment in Python
- Single line comment using #
- Multi-line comments using the triple single or double quote “”” or ‘’’
Python Single line comment
To write a single line comment in a Python program we use the hash # symbol. Every text content that is written in a line after the # symbol treated as a comment by the Python interpreter and gets ignored.
Python single line comment Example 1
# this is a comment text
Python single line comment Example 2
#this add() function add two numbers def add(a, b): return a+b print(add(3, 4)) # calling add() function and printing result
In the above Python program, you can see that only the text which comes after # symbol treated as a comment.
Python Multi-line comment
If you want to write a long description of the module, function, or code snippet which needs more than one line, then you can use the Python multi-line comment.
However, you can also use the
# symbol to comment on multiple lines but for every line, you need to add the # symbol. So, a better approach would be using a multi-line comment.
To write a multi-line comment in Python we can either wrap the comment with triple single or double quote marks.
Python Multi-line comments Example 1
''' this is a comment which is wrapped with triple single quotation '''
""" this is a comment which is wrapped with triple double quotation """
Python Multi-line comments Example 2
''' this add() function add two numbers and return the sum ''' def add(a, b): return a+b print(add(3, 4))
In this tutorial, we learned how can we write comments in Python, and why it is important to write comments in a Program. Comments are generally used to write alternative information about the code, but they can also be used in code debugging. And many programmers use comments to debug their programs. By knowing how to write a comment you make your code more readable not only for yourself but for others too who will apparently read your Python code.