FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Comments in Python Programming




Although comments do not change the outcome of a program, they still play an important role in any programming and not just Python. Comments are the way to improve the readability of a code, by explaining what we have done in code in simple english. In this guide, we will learn about comments in Python and their types.

Python comments

Before we go through an example of comments in Python. Lets first understand the need of comments in Python or in any programming language.

A comment is text that doesn’t affect the outcome of a code, it is just a piece of text to let someone know what you have done in a program or what is being done in a block of code. This is especially helpful when someone else has written a code and you are analysing it for bug fixing or making a change in logic, by reading a comment you can understand the purpose of code much faster then by just going through the actual code.

Lets see how a comment looks like in Python.

pycharm

# This is just a text, it won't be executed.

print("Python comment example")

pycharm

Types of Comments in Python

There are two types of comments in Python.

1. Single line comment
2. Multiple line comment

Single line comment

In python we use # special character to start the comment. Lets take few examples to understand the usage.
# This is just a comment. Anything written here is ignored by Python

Multi-line comment:

To have a multi-line comment in Python, we use triple single quotes at the beginning and at the end of the comment, as shown below.

'''
This is a 
multi-line
comment
'''

Python Comments Example

In this Python program we are seeing three types of comments. Single line comment, multi-line comment and the comment that is starting in the same line after the code.

'''
We are writing a simple program here
First print statement.
This is a multiple line comment.
'''
print("Hello Guys")

# Second print statement
print("How are You all?")

print("Welcome to newworld") # Third print statement

Output

python

# character inside quotes

When # character is encountered inside quotes, it is not considered as comment. For example:

print("#this is not a comment")

Output:

#this is not a comment







Leave Comment