Skip to content

Comments and Readable Code in Python

Programming Python Difficulty 1/5

Beginning of the lesson

Comments and Readable Code

Write notes to yourself and keep your Python easy to read

Big Idea: Code is read far more often than it is written. A comment is a friendly note inside your program, and a clear name tells you what a value means. Together they make your code easy to read, understand, and fix.
By the end you will be able to:
  • write a comment with the # symbol,
  • choose clear variable names that explain themselves,
  • keep code tidy with spaces and blank lines so it is easy to fix.

1. What is a comment?

A comment is a line (or part of a line) that Python does not run. It is a note for humans. In Python you start a comment with the hash symbol #. Everything after the # on that line is ignored by Python.

# This program greets the user by name
name = input("What is your name? ")   # ask the user to type a name
print("Hello,", name)                 # show a friendly greeting

Expected output (after you type Sam):

What is your name? Sam
Hello, Sam

Notice the two kinds of comment: a whole line that starts with #, and a short note at the end of a line after the code. Python skips both.

2. Python ignores comments

Because Python ignores everything after #, comments never change what your program does. You can even "turn off" a line of code by putting a # in front of it.

# The next line is just a note for people. Python ignores it.
print("Python runs this line")
# print("Python never runs this line")

Expected output:

Python runs this line

The second print is now a comment, so it does nothing. This trick is handy when you want to test one line at a time.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers