Skip to content

Exercise 2 of 10 · Variables

Name Tag

What you will make

A bordered name-tag card with your own name across three of its lines, every one of them fed by a single line of code.

The one new idea: A variable is a reusable name for a value

Real programs mention the same value again and again — a name, a price, a filename. Storing it under one name means you fix it in one place instead of hunting through every line.

Go straight to the code ↓

What a variable is

Typing the same piece of text over and over is where mistakes come from. A variable lets you write it once and refer back to it.

This line makes one:

Python
name = "Sam"

The equals sign is not saying that two things are equal. It is an instruction: take the value on the right, and file it under the label on the left. From then on, writing name anywhere in your code hands that value straight back to you.

Think of a labelled drawer. You put something in once. Anyone who opens the drawer later gets whatever is sitting in it at that moment. Swap the contents, and every later opening finds the new thing — you do not have to go round telling each person separately.

One detail decides everything here: the quotes.

  • print("name") prints the four letters n, a, m, e. Quotes mean this exact text.
  • print(name) prints what is stored under that label. No quotes means go and look this up.

A worked example

A different program, so the answer to this exercise is not given away:

Python
city = "Pune"
print("Weather report")
print("City:", city)
print("Have a good day in", city)

Running it prints:

Output
Weather report
City: Pune
Have a good day in Pune

The value "Pune" is written once, at the top, and used on two later lines. Change that first line to city = "Nagpur" and both of those lines say Nagpur instead. The print lines themselves never need editing — they only ever ask for whatever city is holding.

Notice the comma inside those print lines. print is happy to show more than one thing at a time: separate them with a comma and Python prints them in order with a single space between. That is why print("City:", city) comes out as one tidy line rather than two.

Your turn

The editor already prints a name-tag card: a border, a header, a greeting, an owner line and a signature. Run it first, before changing anything, and you will see the card appear with the placeholder in it.

Now find the short line near the top:

Python
name = "YOUR NAME"

Replace YOUR NAME with your own name, keeping the quotes, and press Run again.

Three lines of the card change at once, because all three ask the same variable for its value. You edited one line and the card followed.

Try it a second time with a nickname. That is the whole idea of a variable, and it is worth feeling rather than just reading.

If you want to go a little further, look at the border row — the long line of = signs typed out three times. You could give that a name too, in exactly the same way, and use the name in those three print lines. The card would come out identical.

If something goes wrong

The most common slip is quoting the label by accident. If a print line says print(" Welcome", "name"), your card greets someone called name, because quotes turn it into ordinary text rather than a lookup.

The other one is dropping the quotes off your value, as in name = Sam. Python then treats Sam as a second label, goes looking for something stored under it, finds nothing and reports that Sam is not defined. That message always means the same thing: you asked for a label Python has never been given a value for.

Neither is damage. Read the line the message points at, put the quotes back where they belong, and run it again.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Putting quotes around name in the print lines
Quotes mean plain text, so the card prints the four letters n-a-m-e instead of the value you stored. Only the value at the top wears quotes.
Taking the quotes off the value
Written as name = Sam, Python reads Sam as another label to look up, finds nothing stored under it, and stops with a message saying Sam is not defined.
Spelling the label differently in one place, such as Name instead of name
Python matches labels letter for letter, including capitals, so it treats those as two separate things and stops on the line asking for the one that was never set.

Longer explanation: read the full lesson. Want a blank editor instead? Open the Python playground.