You already know how a shell reads commands and how an editor saves a file. Python adds an interpreter: a program that reads Python source and carries out its statements. To run a saved script, the shell command names that interpreter and then the file.
The course uses python3 as the Ubuntu command. A Python source file usually ends in .py. A statement is an instruction in that file; print("greeting") asks Python to display the quoted text. The parentheses contain the argument, the value passed to print. The quotes mark text and are not displayed.
Python also has an interactive mode, where it prints >>> before a statement you type. Those characters are a prompt, like the shell prompt you learned earlier. They are not part of a saved program. You will practise keeping the file contents, launch command, and resulting text separate.
Before you begin
- Recommended first
- An editor that works under pressure, Reading and making files, A script that does one real chore, ASCII and Unicode, Binary, hex, Base64, and URL encoding
- Estimated time
- 15 min reading, 15 min activity. Setup and optional practice may take longer.
- Where you work
- Read and trace the supplied source and input in your browser. A later optional run uses Python 3.12 and its standard library in a fresh directory under your own ~/yanosec-lab. The ~ means your home directory. Keep paired files together and avoid overwriting existing work. No extra packages or network calls are required.
- Without a lab
- Follow each source line against the supplied input and predicted trace. You can complete the same reasoning without running Python; the practical run can wait. Earlier recorded Python results are labeled separately from predictions. Reading works without JavaScript. Answer checking needs JavaScript and a secure context; saving additionally needs usable browser storage.
After this lesson you can:
- Point the shell at a saved .py file with the interpreter command.
- Keep the interactive >>> prompt out of the saved file.
- Use an error display’s filename and line number to choose where to inspect.
01Example
The saved file is greeting.py. Read its one Python statement first, then the separate shell command that would launch it. The output below is predicted from the source; this worked file was not run. The version command asks about the installed interpreter and does not run greeting.py.
Saved file greeting.py:
print("greeting")
Shell command:
python3 greeting.py
Predicted text on its own line:
greeting
A version check is a different command:
python3 --version
Its result identifies the installed interpreter version.02The shell and the Python prompt are different places
The shell command python3 greeting.py passes greeting.py as a filename argument. The interpreter reads the saved bytes from that file. If you instead enter python3 with no filename, it can enter interactive mode and display >>>. A continuation prompt, ..., means an interactive statement needs another line.
Save source as UTF-8, the default source encoding for Python 3.12. Keep explanatory labels such as “Saved file” and output lines out of the .py file. They help you read this lesson but are not statements.
A syntax error means the source breaks a language rule. Python can report the file, line number, source line, and a pointing marker. Start at that location and inspect the surrounding punctuation. A traceback describes the call path for a running program that raised an exception; a syntax-error display is not necessarily a traceback. The supplied error card is an illustration, not a captured error.
Keep saved source, shell invocation, predicted output and unknown interpreter version in separate panes.Model. A teaching model drawn by hand. It shows the idea, not real tool output.
Read the full explanation below.
Playback is optional. Step through at your own pace.
Instruction being read
python3 greeting.pySaved greeting.py
print("greeting")source
Predicted program stdout
not yet modeledunobserved
Interpreter context/version
shell invocation; installed version unknownunknown
The shell command names the saved source file.
Follow the explanation
- The shell command names the saved source file.
- The source print predicts greeting on its own line.
- --version asks for the installed interpreter version; no number is supplied.
Read every state and value
1. The shell command names the saved source file.
- Current command
- python3 greeting.py
- Data in each location
- Pane: source; Text: print("greeting"); Status: source · Pane: Predicted program stdout; Text: not yet modeled; Status: unobserved · Pane: Interpreter context/version; Text: shell invocation; installed version unknown; Status: unknown
- Links
- From: python3 greeting.py; To: source; Label: names saved file
2. The source print predicts greeting on its own line.
- Current command
- python3 greeting.py
- Data in each location
- Pane: source; Text: print("greeting"); Status: source · Pane: Predicted program stdout; Text: greeting ; Status: predicted · Pane: Interpreter context/version; Text: shell invocation; installed version unknown; Status: unknown
- Links
- From: source; To: Predicted program stdout; Label: predicted print
3. --version asks for the installed interpreter version; no number is supplied.
- Current command
- version
- Data in each location
- Pane: source; Text: print("greeting"); Status: source · Pane: Predicted program stdout; Text: greeting ; Status: predicted · Pane: Interpreter context/version; Text: version-check result not observed; Status: unknown
- Links
- From: version; To: Interpreter context/version; Label: version evidence would be required
03Save hello.py and predict its one line
First practise with a fresh paper case: a saved file called status.py contains the same print statement as greeting.py. You are changing the filename for this prediction, not editing a download. Then inspect hello.py and the error card as separate source-reading tasks.
print("hello")
YanoSec / Code
Illustrative card only. This is not an observed traceback and not the
wording from Python's tutorial example.
Documented shape in the Python 3.12 tutorial, section 8.1:
a File line, the source line, arrows, and SyntaxError.
The file name and line number say where to look.
Original defective source for this card:
print("hello)
What to see: the opening quote has no closing quote before the parenthesis.
Copy a real traceback from a later run if you make one. Do not treat this
card's layout as that program's exact words. Fix a disposable copy only.
Download the illustrative error card, not a captured traceback
- In notes, write the launch command for status.py by placing the interpreter name before that filename. Compare with python3 status.py. The predicted displayed word remains greeting because the statement, not the filename, chooses it.
- Read the downloaded hello.py. Identify its print call and its quoted text. Confirm that the file contains no >>> prompt or explanatory labels. Do not treat its filename as the filename in the first prediction.
- On error-example.txt, find the opening quote with no closing partner. Draw a closing quote before the parenthesis in your notes. The text card stays unchanged.
- Stop after these predictions, or later save hello.py in a fresh directory under your own ~/yanosec-lab and launch it using the interpreter-plus-filename rule. A 2026-10-12 run with Python 3.12.3 recorded hello and exit status 0, meaning successful termination. Your installed version must be checked separately with python3 --version; no install is required here.
04Optional challenge
Spend about ten extra minutes reasoning about a disposable copy with its closing quote removed. Predict a syntax error before trying anything. In the earlier 2026-10-12 Python 3.12.3 run, that defect reported SyntaxError: unterminated string literal (detected at line 1), exited with status 1, and the copy was removed. That record does not make the illustrative download a captured traceback. If you later try a copy yourself, record its actual message and restore the quote before another run.
05Fresh cases for the checks
Use these fictional cases for the checks below. They have different inputs from the worked examples. Read them here or make notes; no installation or external service is needed.
Python launch case AOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.
Current shell directory contains saved file lantern.py.
Source intended for the file: print("lit")
A learner copied this from an interactive session as >>> print("lit").
Illustrative error location from a separate file: File "report.py", line 7.
These are source-reading cases, not a captured execution.
06Explain your prediction
Optional written self-check: Explain the difference between a shell command, a saved Python statement and an interactive prompt. Keep the source prediction separate from an observed run and use an error’s own file/line context.
07Common mistakes
If the file cannot be found, compare the shell working directory with the saved file location. Use a fresh lab directory rather than creating copies in system folders.
If a saved file begins with >>>, remove the prompt characters from your own copy. They belong to the interactive display rather than the program.
If python3 is unavailable, finish the source predictions. Interpreter command names differ across systems; do not rename the file to python or install a package to answer the question.
References
- Python 3.12 tutorial: Using the Python Interpreter
Sections 2.1 and 2.1.1 distinguish a saved-file launch from the interactive prompt; section 2.2.1 covers source encoding.
- Python 3.12 tutorial: Errors and Exceptions
Section 8.1 explains the location and pointing marker in a syntax-error display.