First Steps

Comments in Python

Learn about comments in Python and how to use them to structure and describe our code.


Let's talk about comments in your code.

A very important aspect of programming is documenting what you are doing.

In Python, you can add comments to your code to make it more understandable.

You can add a comment using the # symbol.

# this is a comment print(10)
Python

Everything after the # symbol is ignored when we run our code.

The following code will throw an error because num is not really created:

# num = 10 print(num)
Python
Output

What will be the output?

price = 1 # price = 10 print(price)
Python

You can even add comments at the end of an existing code line. This is called an inline comment:

print(num) # I am an inline comment
Python

Using comments, we can structure our code and make it very clear what our program is doing. Here's an example:

# 1. Declare variables: price = 10 # the price of our product quantity = 5 # the ordered quantity # 2. Updating variable values: quantity = 10 # the customer has doubled the order # 3. Printing the values: print(price) print(quantity)
Python