Python comments: explaining and optimizing code readability

Annotations can be used to explain Python code. Annotations can be used to make the code more readable. Annotations can be used to prevent execution when testing code.

Create comments

Annotations start with #, Python will ignore them:

Example: Get your own Python comments.

#this is a comment 
print("Hello, World!")

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

Example.

print("Hello, World!") #this is a comment 

Annotations may not necessarily be text that explains code, but can also be used to prevent Python from executing code:

Example.

# print("Hello, World!")
print("Cheers, Mate!")

Multiple line comments

Python actually does not have syntax for multi line comments.

To add multiple lines of comments, you can insert a # for each line:

Example.

#this is a comment 
#written in 
#places with more than one row 
print("Hello, World!")

Alternatively, although not the original intention, you can use a multi line string.

Since Python ignores string literals that are not assigned to variables, you can add a multiline string (triple quotation marks) to your code and place comments within it:

Example.

"""
 this is a comment 
 written in 
 places with more than one row 
"""
print("Hello, World!")

Finally

For the convenience of other devices and platforms to watch previous articles: