bhylu.com

Advertisements

Comments

A comment records a reason that would otherwise be easy to lose. Good comments explain a decision or an assumption instead of repeating the next line in different words.

Understand the idea

The hash character starts a comment outside a string. Python ignores the rest of that line as an instruction. Here the comment explains why the total is in integer cents. Keep comments updated as code changes; an old explanation can be more misleading than no explanation.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

# Keep checkout amounts in cents until display.
unit_cents = 275
print(unit_cents * 4)

Read the result

The output is 1100. The comment explains the unit of the amount without taking part in the calculation.

Try one change

Write one useful comment for a value measured in minutes. Remove a comment that merely says multiply the numbers.