0%

Values and Variables · practice

Give a Value a Name Using Variables

Values become much more useful when a program can remember them. In Python, we do that by giving a a name.

greeting = "Hello!"

This instruction creates a named greeting and assigns the "Hello!" to it.

A variable is a name that refers to a value. After the , you can use the name wherever Python expects that value:

Try it

Python follows the name greeting to its value and prints Hello!.

Read an assignment from right to left

Take another look at this line:

city = "Chicago"

Python handles it in this order:

  1. Read the value on the right: "Chicago".

  2. Assign that value to the name on the left: city.

You can read the line aloud as “assign the value Chicago to city.” Programmers often say “city gets Chicago” for short.

The equals sign here means assignment. It tells Python to make a name refer to a value. It does not make a permanent mathematical statement.

A name is different from text

Compare these two print instructions:

Try it

print(message) uses the value stored under the name. print("message") prints the literal text between the quotation marks.

This is one of the first distinctions worth checking when output surprises you:

  • No quotation marks means use a variable.

  • Quotation marks mean use the text exactly as written.

What will this program print?

snack = "apple"
print(snack)

Watch a variable appear

The full playground for this lesson contains an assignment followed by a print instruction. This is a good moment to use the debugger again:

  1. Open the debugger from the editor toolbar.

  2. Move forward until the assignment to message has run.

  3. Look in the Locals panel. The name message now appears beside its current value, "Change me.".

  4. Move past print(message). Only then does the text appear in the output.

  5. Try Prev or the step slider. The variables and output return to their earlier state as you move backward.

Nothing extra is happening behind the scenes. Python creates the variable when it runs the assignment, then uses its value when it reaches print(message).

Change the value in the playground

The starter program creates a variable named message, then prints its value.

Change only the value assigned to message so the program prints:

Variables remember values.

Keep the variable name and the print(message) instruction. This time, the useful change belongs on the assignment line.

Press Run and inspect the output. When it matches, press Submit.

If Python reports a , compare the spelling and capitalization of message in both places. A variable name has to match exactly.

If you use Guided, you can ask Monty:

Explain the difference between message and "message" using a new example. Do not change my code.

The important idea is simple but powerful: a program can attach a meaningful name to a value, then use that name later.

Task

Change the assigned to message so the program prints exactly:

Variables remember values.

Keep the name message and the existing print(message) instruction.

Before submitting, use Step to watch message appear in the Locals panel and the text appear in the output.