Python Comments



Comments can be used to explain Python code.

Comments can be used to prevent execution when testing code.

Comments can be used to make the code more readable and Comments help us understand the code better.

Development environments, debugger and other tools use it

Creating a Comment

Start comments with #, python will ignore rest of line.

Example:-

# I'm Comment
print("Hello, World!")

Comments can be placed at the end of a line, and Python will ignore the rest of the line :-

Example:-

print("Hello, World!")   # i'm comment
Multi Line Comments

To add a multiline comment you could insert a # for each line :-

# This is first comment
# This is second comment
# this is third comment
print("Hello, World!")

If you don’t intend to write ### , you can use a multiline string.

You can include a “documentation string” as the first line of a new function or class you define

Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) to your code, and place your comment inside it:

"""
This is a comment
written in
more than just one line
"""
print("Hello, World!") 


2 thoughts on “Python Comments

Leave a comment