Chapter 1

Write and Run a Small Program

Write and run a small Python program that finds the mean of four temperature readings. Predict its output, change one reading, and use a short error message to repair a misspelled name.

Suppose we have four temperature readings and want their mean. We can calculate the answer by hand, then ask a computer to carry out the same steps. Beginning with a question we already understand gives us a result we can use to check our first program.

The examples run in the browser, so local setup is not a prerequisite. We keep one short temperature report throughout the chapter: first run it, then read its instructions in order, change one reading, and finally repair one misspelled name. Keeping the calculation fixed makes each new programming idea easier to see.

This chapter introduces only the language needed to describe visible behavior: program, source code, output, print, comment, error, and traceback. It does not try to explain every Python symbol at once. The next chapter returns to the same report and explains names, numbers, text, and assignment more carefully.

After this chapter

  • Run a small Python program in the browser and distinguish its source code from its output.
  • Trace the program from top to bottom and predict its labelled output before running it.
  • Change one temperature reading and compare the predicted total and mean with the rerun.
  • Use the source location and final line of a short traceback to repair one misspelled name.

Lessons

  1. 01
    From a Question to a Running Program

    Calculate a small mean by hand, run the same calculation in Python, and distinguish source code from output.

    1 exercise
  2. 02
    Read and Predict a Small Program

    Trace a short program in source order and predict which instructions produce visible output.

    1 exercise
  3. 03
    Change One Thing and Run Again

    Make one controlled change, predict its effect, and inspect the labelled results after rerunning the program.

    1 exercise
  4. 04
    Read and Repair One Failure

    Use a short traceback to locate and repair one misspelled name, then verify the corrected report.

    1 exercise

Review and practice

  1. Review

    Review how a small Python program moves from a hand calculation through output, one controlled change, and one verified repair.

  2. Exercises

    Practise predicting reports, tracing output, changing one value, choosing a useful comment, and repairing a misspelled name.

Optional and reference

  1. Run Python on Your Computer

    Check for local Python, save a small script, and run it from macOS, Linux, or Windows without blocking browser study.

  2. Interactive Python Prompt

    Recognize the Python prompt, run one instruction at a time, and choose when a saved script is clearer.

Chapter progress