Review

Review the chapter's central working method: predict a result, run the code, inspect output or an error report, revise one relevant part, and compare the next result. Revisit scripts, interactive sessions, terminal commands, tracebacks, intermediate output, and Python comments.

This chapter established a complete way to work with a small Python program. We can write or edit source code, ask Python to run it, inspect the result, and use what we observe to choose the next change.

The details vary between a saved script, an interactive prompt, and a browser editor, but the reasoning remains the same. Python runs the instructions it receives. We compare the resulting output or error report with what we expected.

From Source Code to a Result

A Python program begins as source code. The rules that determine how Python code must be written and arranged are called its syntax. A Python interpreter reads source code written with that syntax and attempts to execute its instructions.

The code can reach the interpreter in two forms introduced in this chapter:

  • A script is a saved .py file that can be run again.
  • An interactive session accepts a command at the >>> prompt and shows the result before accepting another command.

The prompt marker is part of the interactive environment, not part of Python source code. A transcript can contain code, displayed values, and prompt markers; a script contains only the code.

Read What the Run Produces

After a program runs, we first separate ordinary output from an error report.

  • Ordinary output includes text and values produced by calls such as print().
  • A traceback reports where Python was running when an error stopped the current run.

For ordinary output, compare the actual result with the result you expected. For a short traceback, begin with the final error category and message, then look upward for the relevant source line.

An execution environment can add status messages or internal traceback locations. In the browser editor, the location marked <lesson> refers to the code being run from the lesson.

Use the Run–Inspect–Revise Cycle

The full cycle is:

predict -> run -> inspect -> change -> rerun -> compare

A prediction gives the output something to be compared with. If expected and actual output differ, the difference identifies an assumption worth checking.

Two techniques make the cycle easier to follow:

  • Print labeled intermediate values to see where a calculation changes.
  • Add a comment when a reason or expectation is not clear from the code alone.

In Python, # begins a comment when it appears outside quoted text. Python ignores the rest of that line. A comment can explain why an experimental value was chosen, but it should not merely repeat an obvious instruction.

Changing one relevant part at a time keeps the cause of a new result visible. Larger changes are still possible; they are easier to understand when divided into smaller experiments.

Commands Used in This Chapter

The available command name depends on the operating system and Python installation. Use the form that successfully identified Python 3 on your computer.

CommandPurpose
python3 --versionask python3 to report its version
python --versionask python to report its version
py --versionask the Windows Python launcher to report its version
python3 hello.pyrun hello.py with python3
python hello.pyrun hello.py with python
py hello.pyrun hello.py with the Windows Python launcher
pwddisplay the current folder on macOS or Linux
lslist files on macOS or Linux
Get-Locationdisplay the current folder in Windows PowerShell
dirlist files in Windows PowerShell

The version-checking command and script-running command should use the same working Python command. If python3 --version identified the intended interpreter, for example, run the script with python3 hello.py.

Common Confusions

What happensWhat to inspect
The terminal cannot find the Python commandTry the command that matches the verified installation.
Python cannot find the scriptCheck the current folder and the exact filename.
A script fails near >>>Remove the interactive prompt marker from the file.
A name is reported as undefinedFind where that name should receive a value before it is used.
A traceback contains many locationsStart with the final error line, then find the location belonging to your source code.
Output differs after several editsReturn to the last understood version and apply one relevant change at a time.
A comment describes an old value or behaviorUpdate or remove the comment when the code changes.

These are different problems. Reading the exact command, filename, source line, or error message is more useful than making unrelated changes.

Check the Main Ideas

The chapter's central ideas are in place if you can:

  • explain the difference between source code and output;
  • run a small .py script with a verified Python 3 command;
  • use the interactive prompt without copying >>> into a script;
  • match each printed line to the instruction that produced it;
  • separate ordinary output from a traceback;
  • identify the category, message, and relevant line of a simple error;
  • predict a result, change one part of the code, and compare the next result;
  • explain why a # comment does not become program output.

This list describes the skills practised in the chapter. It is not necessary to memorize every terminal command; it is more important to know how to verify the command that works in the current environment.

Revisit the Smallest Useful Example

If one part of the cycle is still unclear, return to a program whose result can be checked without Python:

Revisit the complete cycle

Predict both output lines, run the program, and then change either the starting value or the addition.

Ready to run.

The labels make the starting and calculated values visible, while the comment records the expectation being tested.

The exercises that follow combine the chapter's commands, prompt notation, output, errors, and controlled edits. After those exercises, the next chapter examines names, values, and types more closely: how assignment connects a name to a value and how a value's type affects the operations Python can perform.

Review

Not marked done.