From a Question to a Running Program
Begin with four temperature readings, calculate their mean by hand, and run a Python program that produces the same result. Keep the written source code separate from the output it displays.
Four temperature readings are 18, 21, 20, and 23 degrees. What is their mean? This is an ordinary arithmetic question, so we can work it out before asking a computer to do anything.
Work It Out by Hand
First add the four readings:
Then divide the total by the number of readings:
The mean is therefore 20.5 degrees. We can use this hand calculation to check the program's result.
Run the Same Calculation
The following Python program carries out the same steps. Select Run and compare its result with the value we calculated by hand.
A first temperature report
Run the program and compare its three output lines with the hand calculation.
Ready to run.
The text in the editor is the source code. Source code is the text from which Python reads its instructions. When Python runs this source, the program displays three lines:
total: 82
count: 4
mean: 20.5
These lines are the program's output. The source describes a calculation; the output shows selected results from that calculation.
Read Only What We Need
We do not yet need a name for every symbol in the program. For now, notice its larger shape:
- The first three lines calculate and remember
total,count, andmean. - The last three lines use
print()to display those results. - The displayed mean agrees with the hand calculation.
A program is a set of instructions that a computer can run. Python is the programming language in which these instructions are written. The browser provides the Python software needed to run them, so nothing needs to be installed for this lesson.
Q1. Separate the program from its result
Which item is output produced by the temperature program?
Select one choice, then check.
HintLook for the displayed result
The output contains the label and calculated value, but not the Python instruction that produced them.
SolutionThe output is the labelled value
mean: 20.5 is output. The other two choices are lines of source code.
We began with a familiar question, calculated its answer by hand, and ran a Python program that produced the same result. The source code and its output are different parts of that process. Next we will follow the source in order and predict every output line before running it.