Python Variables

Let’s look at variables

  1. Notice

    A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
total = 10
total = total + 2
shopping_list = ["tea"]
basket = shopping_list
basket.append("milk")
print(total)
print(shopping_list)
Live state

Top-level scope

total→Object 2
shopping_list→Object 3
basket→Object 3
Object 2 · int12Object 3 · list['tea', 'milk']
This step

A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.

Two names, one list

Appending through basket changes the list that shopping_list also refers to. Assignment adds a reference; it does not copy the list.

Image brief

Alt text
shopping_list and basket both refer to the same list containing tea and milk; there is no copied list.
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Variables". Teaching focus: Appending through basket changes the list that shopping_list also refers to. Assignment adds a reference; it does not copy the list. Depict exactly the relationships and labels in this reference diagram: shopping_list ----+ | +--> ["tea", "milk"] | ONE LIST basket -----------+ Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: shopping_list and basket both refer to the same list containing tea and milk; there is no copied list.

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
total = 10
total = total + 2
shopping_list = ["tea"]
basket = shopping_list
basket.append("milk")
print(total)
print(shopping_list)
Live state

Top-level scope

total→Object 2
shopping_list→Object 3
basket→Object 3
Object 2 · int12Object 3 · list['tea', 'milk']
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
12
['tea', 'milk']

What do you expect this program to print?

What does this example display?

total = 10
total = total + 2
shopping_list = ["tea"]
basket = shopping_list
basket.append("milk")
print(total)
print(shopping_list)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

12 ['tea', 'milk']

Follow the printed values in order. The output is: 12 ['tea', 'milk']

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.

A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.

After basket.append("milk"), what happens to shopping_list?

After basket.append("milk"), what happens to shopping_list?
Choose one answer. You can try again.

Answer and explanation

Both names refer to the same list object.

Both names refer to one mutable list. Appending through one name changes that shared list; assignment did not copy it.

What is a Variable?

A variable name refers to an object.

A name lets you refer to an object, such as a number or a string.

For example, you can create a variable called name and refer to your name with it:

name = "Arjun"

Here, name is the variable, and "Arjun" is the string object that the name refers to.

Another Example

Let's save a number in a variable:

age = 25

Now, the name age refers to the integer 25.

How to Use Variables

After you create a variable, you can use it whenever you want. For example:

name = "Arjun"
age = 25

print(name)
print(age)
Arjun
25

Important Rules for Naming Variables

  • Names can only have letters, numbers, and underscores (_).
  • Names cannot start with a number.
  • Names should not have spaces. Use _ instead. (Example: my_age)
  • Pick names that make sense! (Example: price is better than p.)

What to remember

Connect what you noticed in the example with the idea below.

Remember

A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.

Watch out for this idea

Assigning a list to another name makes an independent copy.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Constants →