Python – Comments

In Python (or any other programming language), comments are used to explain the source code. Comments describe the code, which helps in maintaining the application in the future.

# prints 4
print(2 + 2)

print(2 + 3)	# prints 5

"""
prints the sum of
two numbers which are 2 and 2
"""
print(2 + 2)

Types of comments in Python

Python supports writing simple single line comment as well as multi line comment as well.

Single line comment

A simple single line comment will start with a hash (#) character. Python runtime ignores any text written after the # character and treats as comment.

# This is a simple comment
print(2 + 2)

print(2 + 3)	# prints 5

Multi line comment

Python does not have anything special to write a multi-line comment. To write it, we can write multiple single-line comments.

I recommend this form of comment.

# This statement
# prints the sum of
# two numbers which are 2 and 2
print(2 + 2)

We can take advantage of a multi-line string literal (using triple quotes), which is not assigned to a variable. Python ignores string literals that are not assigned to any variable, and so it will not affect the program execution.

"""
This statement
prints the sum of
two numbers which are 2 and 2
"""
print(2 + 2)

Drop me your questions related to writing comments in python.

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial