Editing, Commenting, and Rerunning Python Code
Use a controlled editing cycle: predict a result, run the program, change one part, and compare the new output with your prediction. Add Python comments and labeled intermediate output to make the purpose of an experiment easier to follow.
Once a program can run, we can learn from changing it. The useful question is not only whether the new program works, but why its result changed.
A short editing cycle makes that question easier to answer: predict the result, run the program, change one part, predict again, and compare the new output with the prediction. Comments and labeled output can record what we intended to test.
Predict, Run, Change, and Compare
Begin with a small program:
Before running it, we can predict:
double: 8
Now suppose we change only value = 4 to value = 7. The calculation remains
the same, so the new prediction is:
double: 14
Change one value and compare
Run the program with value set to 4. Predict the result for another value, make that one change, and run it again.
Ready to run.
Because only the starting value changed, we can explain the new output without having to separate several possible causes.
This gives us a repeatable cycle:
- predict what the current code will produce;
- run it and inspect the actual output;
- choose one change and predict its effect;
- run the changed code;
- compare the new output with the new prediction.
The prediction does not have to be correct. A difference between expected and actual output tells us which assumption to inspect.
Print Intermediate Values
A final answer can tell us that a calculation is wrong without telling us where it became wrong. Printing intermediate values makes more of the calculation visible.
The name double stores the result of value * 2. The two labeled lines let us
inspect both the starting value and the calculated value:
value: 7
double: 14
Inspect an intermediate value
Run the program, then change either the starting value or the multiplication. Use both output lines to explain the result.
Ready to run.
If value is correct but double is not, the calculation that creates
double is the useful place to inspect. This is more informative than printing
only the final value.
Add a Comment for the Reader
A comment is source text written for a person reading the program. In
Python, a comment begins with the # symbol when that symbol appears outside
quoted text. Python ignores the # and everything after it on the same line.
The comment records the expectation, but it does not become output:
double: 8
Change a comment without changing the result
Run the program, edit only the comment, and run it again. The output remains the same because Python does not execute the comment.
Ready to run.
A comment can occupy a line of its own, as above, or follow an instruction:
value = 4 # starting value for this experiment
Both forms use the same rule: outside quoted text, # begins a comment that
continues to the end of the line.
A Hash Symbol Inside Text Is Not a Comment
Quotation marks change the meaning of #:
print("# is visible here")
This program prints:
# is visible here
The # is inside quoted text, so it is one of the characters passed to
print(). It does not begin a comment.
Compare the two lines:
The first line runs. The second line is entirely a comment, so Python ignores it.
Write Comments That Add Information
A useful comment explains an expectation, a reason, or a choice that the code alone does not make clear.
Useful:
The comment explains why 4 was chosen.
Less useful:
This comment repeats the instruction without adding information. Clear code usually does not need a comment that merely restates it.
Comments can also become inaccurate when the code changes. If value changes
from 4 to 400, a comment that still calls it a small hand-checkable value
must be updated or removed.
Keep One Cause Changing at a Time
Suppose we change the starting value, the multiplication, and the printed label before running again. If the output is unexpected, we now have several possible causes to inspect.
For a small experiment, it is easier to:
- keep the current working version;
- state or comment the expected result;
- change one relevant part;
- rerun the program;
- compare the result;
- keep the change or restore the earlier version.
This method does not prohibit larger edits. It separates an experiment into steps whose effects we can understand.
Exercise: Change one value
Edit only the starting value so the output contains double: 18.
Change one value
Ready to run.
HintWork backward from the output
The program multiplies value by 2. Which starting value produces 18?
SolutionUse nine
Change the first line to value = 9. Then value * 2 is 18, and the
printed label and calculation remain unchanged.
Exercise: Find the comment
Which line is ignored completely by Python?
Select one choice, then check.
HintLook before the hash symbol
Check whether executable code or an opening quotation mark appears before
#.
SolutionThe full-line comment is ignored
Python ignores # print("ready") because # appears first and is outside
quoted text. In the other choices, either the symbol belongs to a string or
valid code appears before the comment begins.
Exercise: Use intermediate output
The intended result is total: 12. Run the program and use the labeled values
to find and correct the calculation.
Use intermediate output
Ready to run.
HintCheck the inputs before the operation
The output shows the intended price and quantity. Which operation combines
4 and 3 to produce 12?
SolutionMultiply price by quantity
Change the calculation to total = price * quantity. The intermediate
output confirms that the inputs are 4 and 3, and multiplication produces
the intended total of 12.
Carry the Method Forward
We can now make a program easier to inspect: predict its result, print useful values, record a reason or expectation in a comment, change one relevant part, and rerun it. Expected and actual output then tell us whether the change had the effect we intended.
The chapter conclusion brings together this editing cycle with scripts, interactive sessions, ordinary output, and error messages.