More Uses for Comments

Challenge 4-2: Comments

There are many more uses for comments than just documenting your code or leaving instructions. You can also use comments to remove lines from your code, while still leaving them as a reminder so they can be added back later. Take the following code as an example:

i = 1

i = i + 3

i = i * 8

i = i - 6

i = i / 2

print(i)

Output:

13

You might decide that you want to skip one of those steps, but it would be good to keep it around in case you want to add it back in later. One way to do that is by commenting out the line you want to skip:

i = 1

i = i + 3

# i = i * 8

i = i - 6

i = i / 2

print(i)

Output:

-1

Now, the program won't multiply i by 8, but if you ever want it to start doing that again, all you need to do is delete the hashtag. Comments can save you a lot of work deleting and rewriting code if you just want to see what happens if you remove a line or two from your program.

You can also use comments to provide useful info about your program. If you take computer programming classes, you'll usually be encouraged to put some comments at the top of your program to let people reading it know your name, when the program was written, and a brief description of what it does, like this:

# Author: Aprille Ericsson

# Date: 05/03/2021

# Description:

# A simple program that adds two numbers together, and prints the output


def addUp(a, b):

print(a + b)

This will also help you if you're looking at some old code you've written, to remember when you made it and what it was meant to do!