Geek University

Use comments

You can use comments in your Python source code in order to document it. With comments, you can remind yourself what a specific portion of the code does and make your code more accessible to other developers.

Since comments are written inside a file that is going to be executed, Python needs some way to determine that the text you write is a comment, and not a command that needs to be executed. You can mark a text as a comment in two ways:

1. using the hash-mark sign (#) – text that appears after the # sign will be defined as a comment and will not be executed. Consider the following code:

# This is a comment.
print ('Hello world!') # This is another comment.

When we execute the code above, we get the following output:

Hello world!

Notice how only the print function was executed. Python has ignored everything after the # sign.

2. using the three double quotes – if you need to comment out a longer multiline text, you can use three double quotes. Python will treat all text inside the three quotes as a comment. Here is an example:

This is our first program.
It prints the text Hello world! to the screen.
It's not much, but hey, it's something.
print ('Hello world!')

The result will be the same as above:

Hello world!

Subscribe to our Newsletter

Like us on Facebook

Share on Twitter

Developed by Signum Soft