Your first program
Run Python, print output, and read an error message without panicking.
Why it matters
Most of AI engineering, day to day, is a loop. Run some code, read what comes back, change something, run it again. That's true even when an AI assistant writes the code for you. You still have to run it, and make sense of the output and the errors. This lesson is that loop at its smallest.
What you'll be able to do
- Run Python code and show results with print()
- Predict the order a program's lines run in
- Use comments to leave notes Python ignores
- Read an error message from the last line up and fix the cause
What it covers
- A program is a list of instructions
- Top to bottom, one line at a time
- Errors are messages, not failures
- A broken script, fixed step by step
- Worked example: the fixed script
- Three small mistakes everyone makes
A program is a list of instructions
Think of a recipe. It's a list of steps, written in order, that someone follows exactly. A Python program is the same: plain text, with one instruction per line. When you run it, Python reads the instructions and carries them out.
The first instruction to learn is print(). It shows a value on the screen.
Press Run on the cell below. You can edit it and run it again as many times as you like.
print("Hello, world") print("Index Ai Academy is running your code")
Two details matter here:
- The parentheses after
printhold the thing you want shown. - The quotes mark a piece of text. Programmers call text a string.
Without quotes, Python treats what you typed as code to work out:
print("2 + 3") print(2 + 3)
The first line prints the characters 2 + 3 exactly as written. The second does the arithmetic and prints 5.
You can give print several things, separated by commas. It shows them all on one line, with a space between each:
print("Clicks this week:", 1200 + 340) print("Emails sent:", 24000, "Opened:", 5280)
print("5 + 5")
Show the answer
5 + 5
The quotes make 5 + 5 text, not a sum, so Python prints those characters as written: 5 + 5. The quotes themselves aren't printed.
Top to bottom, one line at a time
Python runs a program from the first line to the last, in order. Each line finishes before the next one starts. Blank lines are ignored, so use them freely to group related lines.
Comments
Anything after a # on a line is a comment: a note for people reading the code. Python skips it completely.
# Weekly report for the spring campaign print("Spring campaign") print("Clicks:", 1540) # from the ads dashboard, Monday to Sunday # print("Conversions:", 61)
The last line didn't print, because the # turned the whole line into a comment. "Commenting out" a line like this is a common way to switch something off without deleting it.
Good comments explain why the code does something. The code itself already shows what it does.
print("A") # print("B") print("C")
Show the answer
A, then C
Lines run top to bottom. The middle line is a comment, so Python skips it: you see A, then C.
Press Next to run one line at a time. The highlighted line is the one about to run, and the output panel grows as each print runs. The comment line is skipped.
Errors are messages, not failures
Everyone who writes code sees errors all the time, including people with twenty years of experience. An error is Python telling you exactly what it couldn't do. The skill is reading it calmly, like a note from a careful colleague.
The two errors you'll meet first:
SyntaxError means the text isn't valid Python, so Python can't understand it at all. Usually something is missing, such as a closing bracket or a closing quote.
print("This line does not run either") print("Weekly report"
Notice that nothing printed, not even the first line. Python reads the whole program before running any of it. A SyntaxError stops it at that reading stage.
NameError means the program is valid Python, but it uses a name Python doesn't know. The usual cause is a typo:
print("This line runs") prnt("This one has a typo") print("This line never runs")
This time the first line ran. Python only discovered the problem when it reached line 2, and it stopped there.
How to read an error
- Read the last line first. It names the kind of error and describes the problem:
NameError: name 'prnt' is not defined. - Find the line number in the lines above it. That's where Python noticed the problem.
- Look at that line, and the line before it. A missing bracket on one line is often reported on the next, because that's where Python realized something was off.
- Fix one thing and run again. Python reports one error at a time, so a program with three mistakes shows you three errors, one after another.
Tip: if you're stuck, paste the error into the tutor panel. You'll get an explanation, not just a fix.
A broken script, fixed step by step
This script is meant to print a short summary, but it has two mistakes:
print("Campaign: Spring Sale") print("Clicks:", 1200 Print("Status: active")
The first run reports SyntaxError: '(' was never closed, and points at line 2. The bracket opened after print on line 2 never closes. Add the ) and run again.
Now a new error appears: NameError: name 'Print' is not defined. Python is case-sensitive: capital and small letters count as different. So Print and print are different names, and only the lowercase one exists.
Worked example: the fixed script
Both mistakes are fixed: the closing bracket is back on line 2, and Print is lowercase on line 3. Run it to confirm.
print("Campaign: Spring Sale") print("Clicks:", 1200) print("Status: active")
Fixing errors one at a time, rerunning after each, is the normal way to work. Each run either succeeds or gives you the next precise clue. Try breaking it on purpose: delete a quote mark and run it, so you recognize that error next time you see it.
NameError: name 'totl' is not defined on its last line. What's the most likely cause?Show the answer
A misspelled name, probably meant to be total
A NameError means Python met a name it doesn't recognize, and a typo is by far the most common reason.
Three small mistakes everyone makes
Quotes change the meaning. Text in quotes is printed as it stands. Without quotes, Python works it out.
print("2 + 2") print(2 + 2)
The first line prints the characters 2 + 2. The second prints 4. Neither is wrong: they ask for different things.
Capital letters count. Python treats print and Print as two different names, and it only knows the first. If you write Print("hello"), you get a NameError, even though a person would read it fine. The same goes for every name you meet in this course.
Start with the last line of an error. Python's error messages can run to several lines, and the top lines are the least helpful. The last line says what went wrong, and the lines above it say where. Read from the bottom, then look at the line it points to. An error isn't a sign you've done badly. It's the computer telling you what it needs, and professional programmers read dozens of them a day.
Practice what you just read
- 9 code examples you can edit and run
- a practice exercise that checks your code
- a 5-question quiz
- 4 review cards for spaced repetition
Try a quiz question
What happens when you run this?
print("Loading")
print("Report ready"