Module 0: Orientation, Python with AI
How Python Programs Run
What happens when you run a Python file, and how to start reading unfamiliar code.
Lesson 1 of 46 in the recommended order · About 25 min (estimate)
On this page
Outcome
By the end of this lesson you can explain, in plain language, what happens between saving a .py file and seeing its output, and you can tell the difference between a program that fails to start, a program that crashes while running, and a program that runs fine but produces the wrong answer.
Why it matters
Most of the Python you touch in this course, and at work, will not start as a blank file. It will start as code someone (often an AI assistant) already wrote. Before you can trust, review, or fix that code, you need a mental model of what actually happens when it runs: which lines execute, in what order, and what "error" actually means in each of the three ways a program can go wrong.
Concept
A Python file is plain text. Nothing happens to it until you ask the Python interpreter to run it. For a complete file, Python first parses and compiles the source before executing its statements. A syntax error in that file prevents its statements from starting. Once execution begins, statements follow the program’s control flow: the simple examples here run in order, while later lessons introduce choices, repetition, and function calls. This is different from entering separate statements at an interactive prompt.
Three different things can go wrong, and it matters which one you are looking at:
- A syntax error means the interpreter could not even understand the file. Nothing runs at all.
- A runtime error (an exception) means the interpreter understood the file and started running it, but hit an instruction it could not carry out partway through.
- Wrong output means the program ran from start to finish with no error at all, and still produced an incorrect result. This is the hardest of the three to catch, because nothing tells you it happened.
Read the code
Here is a small, complete program. Read it top to bottom before you look at what it prints.
title = "IT Support Services"
posted_days_ago = 3
estimated_value = 42000
print(f"{title}, posted {posted_days_ago} days ago")
print(f"Estimated value: ${estimated_value:,}")
Line by line: the first three lines create three names, title, posted_days_ago, and estimated_value, and bind each one to a value. Nothing is printed yet; assignment is silent. The two print lines run in order, each building a formatted string (an f-string) that mixes literal text with the current value of a name, and sends the result to the screen.
One complete file: where did the failure occur?
Source text → parse and compile → execute statements → compare the result with the requirement.
These are three different failure paths, not three stages every run passes through.
Syntax error: before this file starts
Parse or compilation rejects the file
No statements in this file execute
Look for- A syntax error pointing to code Python cannot accept.
Even a valid statement earlier in this same file does not run. Separate interactive submissions and other imported files are different execution units.
Unhandled exception: during execution
Execution begins, then an operation raises
Execution stops if nothing handles the exception
Look for- The exception type and traceback; earlier output may already exist.
Earlier effects are not automatically undone. Later lessons show how code can handle some exceptions.
Wrong result: execution completes
No unhandled exception stops the program
The result still fails the requirement
Look for- A mismatch with an expected result or a test.
Finishing without an exception is not proof of correctness. A correct run needs the result check too.
Predict the output
Before running the program above, write down what you expect the two printed lines to look like, including punctuation and the comma in the dollar amount.
Check what it actually prints
IT Support Services, posted 3 days ago
Estimated value: $42,000
The :, inside the second f-string's {estimated_value:,} is a formatting instruction, not part of the value. It tells Python to insert thousands separators when it converts the number to text.
Modify the code
Make one small, deliberate change to the program above: replace {estimated_value:,} with {estimated_value:,.2f} in the second print line. Predict the new output before you read on.
What changes, and why
Estimated value: $42,000.00
.2f means "format this as a fixed-point number with two digits after the decimal point"; the , before it still asks for thousands separators. The stored value never changed, only the way it was rendered into text. Keeping that distinction clear, the value versus its presentation, saves a lot of confusion later when a number looks wrong on screen but is actually correct in memory.
Debug the bug
An AI assistant produced the version below. It was asked for the same three lines as above, but it does not run.
title = "IT Support Services"
posted_days_ago = 3
days_remaining = 30 - posted_days_ago
print(f"Days remaining to respond: {days_remaining}"
What's wrong, and why
The last line is missing its closing parenthesis. print( opens a function call that never closes, so the interpreter reaches the end of the file still expecting more. This is a syntax error: the program never starts running at all, not even the first line. The fix is to close the call: print(f"Days remaining to respond: {days_remaining}").
This is a common shape for AI-generated code to get wrong when a suggestion is truncated or edited by hand afterward. Always check that every opening (, [, and { has a matching close before you assume a bug is something deeper.
Try it yourself
Now run some Python for real, in this page. The starter code below already defines title, posted_days_ago, and estimated_value. Add a line that computes how many days are left if opportunities close 30 days after posting, and a line that prints it. Reuse posted_days_ago rather than typing 3 again.
The first run takes a few seconds while the Python runtime downloads; after that it is instant. Nothing you type here leaves your device.
Loading this exercise…
Practical challenge (optional)
Optional: extend the notice/status program so it also prints a warning line ("Review needed") whenever the status is anything other than "Active". You will have the tools to make that decision (if/else) starting in Module 2. If you already know how, try it now; otherwise, skip this and come back after that module.
Sign in to track your progress on this exercise.
AI collaboration
Checkpoint
Without running any code, decide whether each of the following is a syntax error, a runtime error, or neither:
print("Opportunity:" title), aprint(...)call with two things inside it and no operator joining them.print(10 / posted_days_left), whereposted_days_leftwas never assigned a value earlier in the file.- A program that runs to completion and prints
$42000when the correct value was$420,000.
Answers
- Syntax error. Python cannot parse
titlesitting directly after the string with no comma or operator between them. - Runtime error. The name
posted_days_leftdoes not exist yet when that line runs, so this raises aNameErrorpartway through execution. - Neither. The program ran successfully and printed something; the value is simply wrong. This is the "wrong output" case from the Concept section, and it is the one you have to catch yourself.
Sign in to track your progress on this exercise.
Summary and next step
You now have a working model of what "running a Python program" means, and a vocabulary, syntax error, runtime error, wrong output, for describing what went wrong when something does not work. The next lesson in this module continues the same read/predict/modify/debug workflow and introduces the four AI-assisted collaboration habits you will reuse for the rest of the course.