You have attempted 1 of 5 activities on this page
🚫 Log-in to Remove Ads! 🚫
Saving and Logging are Disabled
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
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.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
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?