3.3. Adding Comments

As programs get bigger and more complicated, they get more difficult to read. Good programmers try to make their code understandable to others. However, even with Python, it is still tricky to look at a large program and figure out what is going on.

The best coders add notes to their code to clearly explain what the program is doing. These notes are called comments.

A comment is text within a program intended only for a human reader—it is skipped when the program runs. In Python, the # character starts a comment, and the rest of the line gets ignored.

3.3.1. Try It!

Experiment by adding and removing comments to the code.

Notice that when you run the program, it still prints the phrase Hello, World, but none of the comments appear. Also, notice the compiler ignores the blank lines left in the code.

Comments and blank lines make your programs much easier for humans to read. Use them often!

3.3.2. Check Your Understanding

Question

What are comments for?

  1. To tell the computer what you mean in your program.
  2. To help people reading your code know what the program is doing.
  3. Nothing, they contain information that is not needed.
  4. Nothing in a short program. They are only needed for really large programs.