Lesson 1 of 12
Structured learning draftRun a Python file and inspect the result
Start with a small stock calculation that you can check by hand. Learn the difference between writing a file, running it and reading its output, so a setup problem does not become a programming mystery.
Learning objectives
- Save and run a Python file.
- Distinguish source code from output.
- Check a calculation independently.
Run a Python file and inspect the result: the working sequence
Trace the three stages of run a python file and inspect the result and compare them with the example.
stock.py contains instructions saved as plain text.
Python executes the expressions and assigns the resulting values.
print() displays the remaining count for comparison with a hand calculation.
Save the following code as stock.py and run it with Python 3. Assignment binds a name to a value; print() displays the result. The calculation uses fictional counts and has no external dependencies.
opening = 12
received = 5
sold = 4
remaining = opening + received - sold
print(f'Remaining items: {remaining}')Remaining items: 13Try it, then change it
- Run python stock.py, or python3 stock.py if your system uses that command.
- Change sold to 7 and predict the count before running again.
- If the file cannot be found, check the terminal's folder and exact filename, including its extension.
Common mistakes
- Saving the file as stock.py.txt.
- Typing the example's output back into the program.
- Running a different saved copy from the one being edited.
Practice activity
Apply the lesson
Create a file that calculates remaining library books from an opening count, returns and loans. Include your expected result and the command you used to run it. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · Python
The script stops with a traceback or gives an unexpected value.
- Find the cause
- Read the exception type and message on the last line, then find the referenced line in your own file. Compare the value and type of each input with what that operation expects.
- Try this
- Reduce the failing case to a few lines. Check variable spelling, numeric conversion and file paths. Handle only the exception you expect; do not hide an unknown error with a broad except block.
- Check the fix
- Run the corrected case and a deliberately invalid input. The valid input produces the expected result; the invalid input gives a clear message or raises the documented exception.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: programming with Python
Choose the lecture on functions, loops, exceptions, tests or files that matches your current task.
Try it yourself: Before running an example, predict its output. Try an empty input and record how the program behaves.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
You changed the code but see the old output. What should you check first?
Lesson summary
- A file must be saved before its changes can be run.
- Source code and displayed output are different things.
- Check a small calculation before trusting a larger one.
Sources and further reading
- An informal introduction to PythonPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.