Comments

In programming, it is important to provide comments in your code to explain what you're doing. This is helpful for both yourself and others who might read your code later.

Comments are not interpreted by Python, so they don't affect how your code runs. Python supports two types of comments:

  • Single Line Comments
  • Multiple Single-Line Comments

1. Single Line Comments

Single-line comments start with a # (hash symbol). Everything following the # on that line will be ignored by Python. For example:

# This is a single-line comment (This won't run).
print("I am learning about comments")

Notice that comments are usually shown in a different color in your code editor.

2. Multiple Single-Line Comments

Python doesn't have a specific syntax for multiline comments. Instead, to comment out multiple lines, you need to place a # at the start of each line. For example:

# This is a single-line comment.
# This is also a single-line comment.
print("I am learning about comments")

If you highlight multiple lines of code and press Ctrl + / (or Cmd + / on macOS), the editor will automatically add a # at the start of each line, turning them into comments.

Note: While you might see triple quotes (''' or """) used for what looks like multiline comments, these are actually docstrings used to document functions or classes. They should not be used for regular comments.

3. A Note on Using Comments

There is some debate about how often to comment, and different programmers have different styles. Here's some basic advice:

3.1 Comment When It’s Useful

For example, this comment is not useful:

# This line prints out Hello World!
print("Hello World!")

Everyone can see what the code does, so this comment doesn’t add value. Only comment when it's not obvious what a piece of code does.

3.2 Be Professional

Your comments reflect on you, especially if others are reading your code. Avoid unprofessional comments, like:

# This code sucks!
print("Hello World!")

or:

# Workaround for Tyrion being a traitor and going off with that dragon lady!
print("Hello World!")

3.3 Keep Comments Brief and Clear

Make sure your comments are concise and to the point. If you need to write a longer explanation, split it into multiple lines. For example:

# The following is a comment that has the job of telling the person reading the source code that this prints Hello World!
print("Hello World!")

would be better as

# Prints Hello World!
print("Hello World!")

or:

# The following is a comment that has the following job.
# To tell the person reading the source code that this prints Hello World!
print("Hello World!")

=== TASK ===

Copy the following code into a new Python file.

# This is a single line comment in Python

print("Replace this line with TASK 1")

print("Hello")
print("World")
print("with Comments")
  1. Modify the code so that the first print statement outputs: Single line comments start with #.
  2. Highlight lines 5-7 and press Ctrl + / (or Cmd + / on macOS) to comment out those lines.

Your final output in the terminal should look like this:

Single line comments start with #