Course outline

Python / Foundations / Lesson 1

Running Python and reasoning about a program

A program is a reproducible sequence of instructions. Establish a reliable interpreter, separate the shell from Python, and trace names and output before learning larger constructions.

About 5 min readContent reviewed
On this page

Lesson goals

  • Distinguish source files, interpreter, shell, and interactive session.
  • Trace assignment and reproduce output in a fresh process.

Source becomes actions

Python source is text read by an interpreter. A .py file preserves that text; running it creates a process with its own names and objects. Statements normally execute in order, so a later statement can use a name assigned earlier. Comments beginning with # explain intent and do not execute. Reading a program means predicting how its state changes, rather than guessing from the last line. Start every experiment with a tiny question such as which value a particular name refers to after assignment. A saved file is easier to reproduce and discuss than an undocumented sequence of interactive edits.

Choose the interpreter deliberately

This course uses Python 3.11 or later and only the standard library in runnable examples. On many Unix systems python3 selects Python; on Windows the installed launcher may provide py. Command names depend on installation, so inspect the version before relying on one. A terminal command starts a program, while the Python prompt accepts Python expressions and statements. Do not paste the prompt characters into a script. An editor saves source but does not necessarily choose the same interpreter as the terminal. Compare their interpreter settings when behavior differs, and keep system installation changes outside a beginner experiment.

Assignment binds names

An assignment evaluates the expression on the right and then binds the name on the left to its result. It is not a mathematical equality that must remain true forever. Reassigning a name changes its binding, and does not revise an earlier numeric result. Descriptive lowercase names make the trace visible: unit_price says more than x. Avoid names such as print or list because they hide useful built-ins. The example records a price and a quantity, calculates a total, and then changes quantity. The total remains the value calculated when that assignment executed; a spreadsheet-style automatic recalculation does not occur.

python Assignment binds names โ€” worked example
unit_price = 7
quantity = 3
total = unit_price * quantity
quantity = 4
print(total)
print(quantity)
Expected output
21
4

Multiplication sees quantity equal to three at the moment total is assigned. The second assignment only changes quantity. Two print calls expose both bindings and make the timeline testable.

Expressions, print, and deterministic checks

An expression produces a value. In an interactive session its representation may appear automatically, but a source file usually needs print to display a result. print separates its arguments with spaces and finishes with a newline unless told otherwise. Output is evidence, and the expected lines are part of an exampleโ€™s contract. Prefer fixed inputs while learning so an unexpected line points to reasoning rather than user typing or clock changes. The second example uses the standard sys module only to test the supported version boundary; it avoids printing a machine-dependent executable path or a complete version string.

python Expressions, print, and deterministic checks โ€” worked example
import sys

name = "Mira"
print("Hello", name)
print("Supported:", sys.version_info >= (3, 11))
Expected output
Hello Mira
Supported: True

Import makes the sys module available under a name. The comparison yields a Boolean, and print formats that Boolean as True on the courseโ€™s supported interpreters.

A reproducible learning loop

Create one source file for one question, predict its output on paper, run it, and compare the exact lines. When the result differs, locate the first statement where your predicted state and actual state disagree. Restart the process after changing the file so stale interactive bindings cannot hide a missing assignment. Keep indentation consistent with four spaces; indentation becomes meaningful when blocks arrive in later lessons. Save files as UTF-8 so Arabic text and other Unicode characters remain readable. This small routine scales: recording the interpreter, input, program, and observed result gives another person enough information to reproduce a report without access to your editor.

Common errors and fixes

NameError after using an unassigned name

Why it happens
The statement reads a name before any binding exists in this process.
How to fix it
Assign the value before reading it, checking spelling and letter case.
Prevent it next time
Run the saved file in a fresh process after every meaningful edit.

SyntaxError after copying terminal prompts

Why it happens
Characters such as >>> are documentation prompts rather than source.
How to fix it
Copy only statements into the .py file.
Prevent it next time
Keep terminal instructions separate from Python source in your notes.

A saved expression displays nothing

Why it happens
Automatic expression display belongs to the interactive session.
How to fix it
Pass the result to print when visible output is required.
Prevent it next time
Predict displayed output separately from values computed internally.

Check your understanding

1. Trace x = 4; y = x + 2; x = 9. What would printing x and y show?

Show worked solution

The first assignment binds x to four. The second computes six and binds y to it. Rebinding x later leaves y unchanged, so print(x, y) produces 9 6. This demonstrates why assignment is an action at one moment.

2. Design a reproducible report for a script that runs differently in an editor and a terminal.

Show worked solution

Record both selected interpreter versions, the saved filename, complete minimal source, invocation method, and exact output. Restart each process and rerun the same file. If the versions differ, select the same supported interpreter before investigating language behavior.

Key takeaways

  • Assignment binds a name when the statement executes.
  • A fresh process and fixed inputs make observations reproducible.
  • Distinguish values computed from output displayed.

Official references