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 #
symbol:
# compute the percentage of the hour that has elapsedpercentage=(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?
To add notes to code.
Comments are used to add notes to code.
To assign names to variables.
Comments are not read by the program when code is running, so variable assignments should not be commented out.
To run code.
Try again! Comments do not run the code.
To keep track of how many times a loop has iterated.
Try again! Please do not keep track of iterations in a comment!
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-3: Which of the following is not true about comments?
Comments start with the symbol #
Try again! This is true! Which of the options is not?
Comments document the non-obvious features of the code.
Try again! This is true! Which of the options is not?
Comments function like any other line of code.
Comments are ignored and have no effect on the program.
Comments cause the rest of the line to be ignored.
Try again! This is true! Which of the options is not?
csp-10-2-4: What will be printed after the following code executes?
name="Milo"age=12sentence=" is this many years old: "#print(name + statement + age)
Milo is this many years old: 12
Try again! Remember that # is the symbol for comments.
Milois this many years old:12
Try again! Remember that # is the symbol for comments.
Milo + is this many years old: + 12
Try again! Remember that # is the symbol for comments.
Nothing will print.
The print statement is commented out, so this code only assigns values to each variable.
2.11. CommentsΒΆ
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
#
symbol:In this case, the comment appears on a line by itself. You can also put comments at the end of a line:
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:
This comment contains useful information that is not in the code:
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-3: Which of the following is not true about comments?
csp-10-2-4: What will be printed after the following code executes?