Signup/Sign In

How to Comment in Python

When programmers write code, they write with their own logic and idea. Programmers may write 100 lines of code or may write 1000 lines of code of the project. Sometimes what happens is that when the other user or the programmer needs that project, he may not understand the logic behind that code or the statements written in that code. In that such scenario, comments are useful.

Comments help the user or a programmer to understand the code and to remember why he wrote or for what purpose he wrote the statements, line of code, or functions.

Example

Let us try to print some statements and write some comments to understand why we write those lines of code.

I am using the print() function to print some statements
print("Hello world!!!!!!")
print("This is my first program")
print("I am learning python programming language")


File "C:/PycharmProjects/pythonProject2/module.py", line 1
I am printing the statement one
^
SyntaxError: invalid syntax

In the above program, we try to print some statements and added the comment to understand why and what function we used. But we got SyntaxError: invalid syntax in the first line of code because the python interpreter could not understand that line.

As we are trying to comment message, but this not the proper way to comment on the message.

How should we write comments?

  • Comments should be short and specific. When the programmer sees those lines of code, he should understand the logic of code.

  • In python, comments start with the # symbol.

  • When we write statements after the # symbol, those lines will never be executed and are ignored by the python interpreter.

Two types of comments in Python

1. single-line of the comment.

2. Multi-line comments.

Let us try to understand by examples.

1. Single-Line Comment

As the name says, it is a single line comment, present with inline code or above the code. This example shows the single-line of comments present above the code. Let's try example 1 again by putting the #(hash) symbol before the line of code.

#I am using print() function to print some statements
print("Hello, world!!!!!!")
print("This is my first program")
print("I am learning the python programming language")

In the above example, only the three-line will be executed. Anything after the hash symbol, python interpreter will ignore.


Hello, world!!!!!!
This is my first program
I am learning the python programming language

Example

When we don't want to execute a particular line of the statement, we can do that by adding the # symbol before the statement.

Let's say we don't want to print the second line in the program.

print("Hello, world!!!!!!")
#print("This is my first program")
print("I am learning the python programming language")

In the above program, the second line will not execute as we added the # symbol before the statement.


Hello, world!!!!!!
I am learning the python programming language

Python Multi-line Comment

Multi-line comment means # symbol present in every line of comment.

Example

If we want to give a comment on multiple-lines, then we need to add the # symbol on every line.

#In this program, we will add two numbers
# This is the value of a
a=100
print("The value of a is:",a)
#This is the value of b
b=200
print("The value of b is:",b)
# Add two numbers a and b
#store in the variable c
#print the value of c
c=a+b
print("The value of c is:",c)

In the above example, we added comments on multi-line. But the python interpreter will ignore the comment lines.


The value of a is: 100
The value of b is: 200
The value of c is: 300

Conclusion

In this tutorial, we learned about the comments, what is the use of comments in python and how to write comments in Python code.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.