Read and Predict a Small Program

Follow the temperature program from top to bottom and predict each line of its report before running it. Track remembered results and visible output as separate parts of the run.

The temperature program contains six instructions. It has no choices or repeated steps, so Python runs these instructions from top to bottom, one at a time. Following that order lets us predict the result before we run the code.

Predict the Report

Here is the program again:

The first line calculates 82 and remembers that result under the name total. The second remembers 4 under count. The third uses those remembered results to calculate 20.5 and remembers it under mean.

The next three instructions display the report. We can therefore predict:

total: 82
count: 4
mean: 20.5

Run the program only after making this prediction.

Check a predicted report

Predict all three output lines, then run the unchanged program and compare.

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

When predicted and actual output agree, we have evidence that we followed this run correctly. It does not prove that every possible program would work, but it does check our reading of this one.

Follow One Instruction at a Time

The following trace keeps each instruction beside the change it causes:

InstructionWhat Python now remembersNew output
total = 18 + 21 + 20 + 23total is 82
count = 4count is 4
mean = total / countmean is 20.5
print("total:", total)no changetotal: 82
print("count:", count)no changecount: 4
print("mean:", mean)no changemean: 20.5

The first three instructions calculate values without displaying them. Output appears only when the program reaches print(). A function is named code that performs a task; print() is a built-in Python function whose task is to display values.

The line-by-line trace is also a useful reading method. Keep three questions separate:

  1. Which instruction runs now?
  2. What result does it calculate or remember?
  3. Does it add anything to the output?

Source and Output Must Stay Separate

Consider this instruction:

print("mean:", mean)

It contains a function name, parentheses, quoted text, and a remembered value. Its output contains only the label and the value:

mean: 20.5

Python does not print the source line itself. It carries out the instruction. Keeping source and output separate prevents us from treating punctuation such as parentheses or quotation marks as part of the result.

Q1. Predict the output in order

What does this program display?

Choose one

Select one choice, then check.

HintFind the print instructions

The calculation of total does not display it. Write one output line for each print() call, in the order in which the calls appear.

SolutionCount appears before the mean

The program remembers 24, 2, and 12.0. It prints only count: 2 and then mean: 12.0.

Not attempted
Review

Not marked done.

Reading this short program is a trace: follow its instructions in order, keep remembered results separate from visible output, and predict each printed line. We will use the same trace in the next lesson while changing one temperature reading.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.