Comments are hints that we can add to our program to make our code easier to understand. Let's take an example.
Suppose we have a program to print a text entered by the user.
name = input("Enter your name")
print(name)
To make this program a bit more readable, we can add comments.
# Program to take the user's name
name = input('Enter your name')
print(name)
Furthermore, comments are completely ignored by the compiler. For example,
# print me
print('Hello')
Output
Hello
As you can see, the comment # print me
is ignored by the compiler
and only the print('Hello')
is executed.
Now, let's see different types of comments in Python.
Python Single-line Comment
In Python, we use the hash(#) symbol to write a single-line comment. For example,
# declare a variable
name = 'John'
# print name
print(name) # John
In the above example, we have used three single-line comments:
# declare a variable
# print name
# John
The Python compiler ignores everything after the #
symbol.
As you can see, we can also write single-line comments in the same line as the code. For example,
print(name) # John
In such cases, the compiler executes the code statement before the
#
and ignores the text after #
.
Note: Remember the keyboard shortcut to apply comments. In most text editors, it's Ctrl + / if you are on Windows & Cmd + / if you are on a Mac.
Prevent Executing Code Using Comments
We can also use comments while debugging our code.
Suppose we get an error while running a program. Rather than removing the code completely, it is a common practice to comment out code so that they are ignored during execution.
Let's see an example.
number1 = 10
number2 = 15
sum = number1 + number2
print('The sum is:', sum)
print('The product is:', product)
If code throws an error because we have not defined a product variable.
Instead of removing the print statement, we can comment the code that's causing the error. For example,
number1 = 10
number2 = 15
sum = number1 + number2
print('The sum is:', sum)
# print('The product is:', product)
Output
The sum is 25
Now the code runs without any error.
Here, we have resolved the error by commenting on the code related to the product. Now, if we need to calculate the product in the near future, we can simply uncomment it.
Python Multiline comments
Python doesn't have multiline comments like other programming languages such as C++ and Java.
However, we can use the hash symbol at the beginning of each line to get the same effect.
# print(1)
# print(2)
# print(3)
We can use triple quotes (''' ''') to achieve a similar effect.
'''This program takes an input from the user
and prints it'''
name = input('Enter your name: ')
print(name)
Output
Enter your name: John John
We can see that these unassigned multiline strings are ignored.
Why Use Comments?
We should use comments for the following reasons:
- Comments make our code readable for future reference.
- Comments are used for debugging purposes.
- We can use comments for code collaboration as it helps peer developers to understand our code.