Skip to content

Module 3 · Variables and Data Types — topic 1 of 15

Variables

A variable is a name that refers to a value stored in memory. Learn how Python variables work as labels, how to create and update them, and how to use them in calculations.

Beginner 12 min read·Lesson 29 of 306 in Python Programming

Learning objectives

  • Explain what a variable is using the label model
  • Create, read and update variables
  • Use variables to make calculations readable

Definition

A variable is a name that refers to a value stored in memory. Learn how Python variables work as labels, how to create and update them, and how to use them in calculations.

Introduction

Programs need to remember things: a student’s name, a running total, the number of attempts left. A variable is a name you choose that refers to a value, so you can use and change that value later in the program.

In Python it helps to picture a variable as a label tied to a value, not as a box that contains it. The value lives in memory; the name simply points at it.

Why it matters

Without variables you would have to repeat every value everywhere and could never keep track of changing information such as a score. Well-named variables also explain a program: tax_amount is far clearer than 0.18 * 2400.

Detailed explanation

You create a variable by assigning a value to a name with =: score = 0. There is no separate declaration step; the first assignment creates the name.

Using the name later gives you its current value. If you use a name that has never been assigned, Python raises NameError.

Assigning again rebinds the name to a new value. The old value is no longer reachable through that name, and Python reclaims its memory automatically if nothing else uses it.

Because a name is only a label, two names can refer to the same value. After a = 10 and b = a, both labels point to the same 10. Rebinding a afterwards (a = 20) does not change b.

A variable can be used in its own update: score = score + 5 reads the current value, adds 5 and rebinds the name to the result.

Read = as “refers to”

score = 10 means “score now refers to the value 10”, not “score equals 10” in the mathematical sense.

Syntax

Python
variable_name = value
variable_name = new_value
  • The name goes on the left of =, the value or expression on the right.
  • The right side is evaluated first, then the name is bound to the result.
  • Reassigning simply points the name at a new value.

Code examples

Create and use

Python
student = "Anil"
marks = 78
print(student, "scored", marks)
Output
Anil scored 78

Update a variable

Python
lives = 3
lives = lives - 1
print(lives)
Output
2

More examples

Labels, not boxes

Python
a = 10
b = a
a = 20
print("a:", a)
print("b:", b)
balance = 1000
for deposit in [250, 400]:
balance = balance + deposit
print("balance:", balance)
Output
a: 20
b: 10
balance: 1650

Step-by-step explanation

  • b = a makes b refer to the same value 10 that a refers to.
  • a = 20 moves only the a label to a new value; b still refers to 10.
  • balance starts at 1000 and is rebound twice inside the loop: 1250, then 1650.
  • Each update reads the old value on the right before rebinding on the left.

Expected output

Running “Labels, not boxes” prints exactly:

TextOutput
a: 20
b: 10
balance: 1650

Practical applications

  • Game code keeps score, level and remaining lives in variables that change every frame.
  • Billing scripts store subtotals, tax and discounts in named variables before printing an invoice.
  • Configuration values such as a server address are stored in variables at the top of a script so they are easy to change.

Best practices

  • Give every variable a meaningful starting value.
  • Use one variable for one purpose throughout its life.
  • Choose names that describe the value, such as total_price, not tp.

Common mistakes

Using a variable before assigning it

Avoid

total = total + 5

Prefer

total = 0
total = total + 5

The first line reads total before it exists, so Python raises NameError. Give it a starting value first.

Writing the assignment backwards

Avoid

10 = count

Prefer

count = 10

The name must be on the left of =. A literal can’t be assigned to.

Key takeaways

  • A variable is a name bound to a value.
  • = evaluates the right side, then binds the name on the left.
  • Reassigning a name doesn’t affect other names that referred to the old value.

Try it yourself

  1. 1
    Create variables for the price of a book and the number of copies, then print the total cost.
  2. 2
    Predict the output: x = 5; y = x; x = x * 2; print(x, y). Then check.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

12 questions cover the “Variables and Data Types” module. Answers and explanations are revealed after you submit.

  • BeginnerWhat happens after x = 10 and then x = "ten"?
  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
Practise this module