Python Comments – Adding comments to code

Comments can be used in a few ways, the first of which is to explain your code, this can be invaluable to any one who may be reading your code. They are also useful in testing, whereby parts of code can be turned in to comments to stop them from being executed.

Comments in Python code start with the hash (#) character, this works on a per line basis. An example of this can be found below.

#Test print hello world
print("Hello World!")

The above returns

Hello World!

In the below we are going to apply a comment hash to our second line.

#Test print hello world
#print("Hello World!")

This prevents the print command from being executed and so the output of the above results in nothing happening.

Leave a Reply