As programs get bigger and more complicated, they get more difficult to
read. Formal languages are dense, and it is often difficult to look at a
piece of code and figure out what it is doing, or why.
For this reason, it is a good idea to add notes to your programs to
explain in natural language what the program is doing. These notes are
called comments, and in Python they start with the #
# compute the percentage of the hour that has elapsed
percentage = (minute * 100) / 60
In this case, the comment appears on a line by itself. You can also put
comments at the end of a line:
percentage = (minute * 100) / 60 # percentage of an hour
Everything from the # to the end of the line is ignored; it
has no effect on the program.
Comments are most useful when they document non-obvious features of the
code. It is reasonable to assume that the reader can figure out what
the code does; it is much more useful to explain why.
csp-10-2-1: What are comments used for?
This comment is redundant with the code and useless:
v = 5 # assign 5 to v
This comment contains useful information that is not in the code:
v = 5 # velocity in meters/second.
Good variable names can reduce the need for comments, but long names can
make complex expressions hard to read, so there is a trade-off.
csp-10-2-2: What is the symbol for a single line of comments?
csp-10-2-3: Which of the following is not true about comments?
csp-10-2-4: What will be printed after the following code executes?
name = "Milo"
age = 12
sentence = " is this many years old: "
#print(name + statement + age)