Variables and print()

15-minute lesson · module-01-getting-started

Practice exercises · earn 350 XP

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Variables and print()

15-minute lesson · module-01-getting-started

  • Understand what a Python program is and how it executes top to bottom.
  • Create string and integer variables and print them.
  • Predict the output of simple programs before running them.
  • Recognize when a NameError happens and what causes it.

A program is a list of instructions

When you run a Python program, the computer reads it from top to bottom and carries out each instruction in order. The very first program almost everyone writes prints a friendly message to the screen.

print("Hello, world!")

The word print is a function — a small named tool that does a job. When you write print(...), you are calling the function and giving it something to display: the value inside the parentheses.

Variables hold values

A variable is a name that points to a value so you can use it later. You make a variable using the = sign:

name = "Cyclone"
year = 2026
  • name is a string — text inside quotes.
  • year is an integer — a whole number, no quotes.

Once a variable is created you can use it anywhere a value is allowed:

print(name)
print("It is the year", year)

Names matter

Python is case sensitive. Name and name are two different variables. If you type a name Python has never seen before, you get a NameError — Python is essentially saying "I don't know what you mean."

Predict before you run. Reading the program and guessing what will happen is the single most useful habit a new programmer can build. The AI tutor on the right will encourage you to predict first — please do!

Examples

Greeting a person

name = "Adisak"
print("Welcome to Python,", name)

`name` stores the string "Adisak". The print function shows two values separated by a comma — Python adds a space between them automatically.

Checkpoint questions

What does this program print?

x = 5
y = 3
print(x + y)

Which line will cause a NameError?

city = "Ames"
print(City)

Hands-on coding

Print a welcome line for ISU CS

Pyodide is loading (one-time, ~12 MB)…

ActiveCode
Output
(no output yet)

CodeLens — step through execution

How variables and print work step by step

CodeLens — step through frames, objects, and references

CodeLens
Code · Python 3
1name = "Cyclone"
2year = 2026
3print("Hello,", name)
4print("It is the year", year)
5
line that just executed next line to execute
Print output
(click Trace)

Frames

Click Trace to start.

Objects

no steps yet
Edit the code

Debugging interlude

Predict the error first (+50 XP), then fix the code (+100 XP). Hints cost 30 XP each.

🐞

Spot the NameError

One of these lines will cause a NameError. Predict which one and why, then fix the program so it prints the greeting correctly.

Predict · 50 XPFix · 100 XP
city = "Ames"
print("Hello from", City)
Step 1 — Predict the error

Read the program. Which kind of error (or non-error) will Python raise?

End-of-lesson quiz

End-of-lesson quiz

  1. Which of these creates a variable that holds the number 42?

Reflection

  • In your own words, what is the difference between a variable and a value?

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · General Introduction / Algorithms / The First Program / Comments

Adaptation notes. Reading rewritten in our own words. Examples and hidden tests authored specifically for PyTutor AI. Pedagogical sequencing follows Think CS Python — variables introduced after print() so the first program is immediately runnable.

License notes. Verify chapter-level license metadata on Runestone before copying any text verbatim. This lesson contains no verbatim quotations.