Lesson 4 of 6 · Getting Started · 2 min read · Updated
Comments in Python
A comment is a note for people. Python ignores it. Learn to write notes and to switch a line off.
Understand
A comment is a note in your code. It is written for people, not for Python.
Python skips every comment. A comment starts with the # sign.
Note
A short note helps you understand your own code when you open it again after a few weeks.
Use it
Here is a program with one comment. The comment sits on its own line.
# Show a greeting
print("Hello")
Hello
Python ignored the first line. It ran only the print line.
A comment can also come at the end of a line. Everything after the # is ignored.
print("Hello") # this note is ignored
# print("Goodbye")
Hello
The second line starts with #, so Python skipped it. Putting # in front of a line is a quick way to switch that line off.
Practice
This code prints two words. Switch off the second line so that only Hello is printed.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
Hello
Need a hint?
- Put a
#in front of the second line. - The second line should look like
# print("Goodbye").
Show a solution
print("Hello")
# print("Goodbye")
The second line is switched off. Switch it on again so that both One and Two are printed.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
One
Two
Need a hint?
- Remove the
#at the start of the second line. - The line should be
print("Two").
Show a solution
print("One")
print("Two")
Write a comment that describes your code. Then print the word Python.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
Python
Need a hint?
- Start the comment line with
#. - On the next line write
print("Python").
Show a solution
# Show the name of the language
print("Python")
Which sign starts a comment in Python?
Create a free account to save your progress, or sign in.