Comments are little texts that can be added in Python code. They are created for programmers to read, not computers. A comment is simply one or more lines of text that is not executed by the computer.

There are two ways to comment in Python: single-line comments and multi-line comments.

Related Course: Complete Python Programming Course & Exercises

Single line comment

A single line comment starts with the number sign (#) character and can only be a single line long. If no single line comment (#) is found on the next line, it continues execution of the program.

  # This is a comment
print('Hello')

For each line you want to comment, put the number sign (#) in front. You can type any text in the comment, it is ignored by Python. Comments exists to assist human developers, not computers.

To demonstrate that, you can type literal code in a comment, but the computer won’t run it, it’ll immediately jump to the next line.

# print('This is not run')
print('Hello')

Comments should help other software developers (or yourself in the future), by making it easier to understand what you are doing in the code. It should not be something obvious like:

typical code comments

Usually single line comments are not enough, many developers leave a lot of text in their code and in order to do that multi line commenting is needed. (or repeat the # for every sentence)

Multiline comment

In Python, multiple lines can be commented created by repeating the number sign (#) several times. The code would look something like this, and while that works, it is very tedious to write comments that way.

# This is a comment
# second line
x = 4

To be clear, programmers are busy people and this quickly becomes impractical. A common way to use comments for multiple lines is to use the (‘’’) symbol, this lets you continue typing text across multiple lines:

  ''' This is a multiline 
Python comment example.'''
x = 5
Download exercises