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
.pyfile 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.
| Command | Purpose |
|---|---|
python3 --version | ask python3 to report its version |
python --version | ask python to report its version |
py --version | ask the Windows Python launcher to report its version |
python3 hello.py | run hello.py with python3 |
python hello.py | run hello.py with python |
py hello.py | run hello.py with the Windows Python launcher |
pwd | display the current folder on macOS or Linux |
ls | list files on macOS or Linux |
Get-Location | display the current folder in Windows PowerShell |
dir | list 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 happens | What to inspect |
|---|---|
| The terminal cannot find the Python command | Try the command that matches the verified installation. |
| Python cannot find the script | Check the current folder and the exact filename. |
A script fails near >>> | Remove the interactive prompt marker from the file. |
| A name is reported as undefined | Find where that name should receive a value before it is used. |
| A traceback contains many locations | Start with the final error line, then find the location belonging to your source code. |
| Output differs after several edits | Return to the last understood version and apply one relevant change at a time. |
| A comment describes an old value or behavior | Update 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
.pyscript 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.