A variable is a named storage location in the computer’s memory that holds a value. You can think of variables like those used in algebra — they represent a value that can change.

x = 10
y = 20
print(x * y)

In this example, we assign the values 10 and 20 to x and y, then print the result of multiplying them together (200).

How Variables Work in Memory

When you create a variable, the computer reserves a space in memory to store its value. The variable name acts as a label for that memory location, so you don’t need to remember complex memory addresses.

band_name = "The Beatles"
print(band_name)

Variable Naming Rules

When creating variable names (also called identifiers), you must follow these rules:

  1. A variable name must start with a letter or an underscore (_)
  2. A variable name cannot start with a number
  3. A variable name can only contain letters, digits, and underscores (A-z, 0-9, _)
  4. Variable names are case-sensitive (age, Age, and AGE are three different variables)

It is also good practice to use variable names that describe their contents. This makes your code easier to understand and debug.

# Good variable names
player_score = 100
user_name = "Alice"

# Poor variable names
x = 100
a = "Alice"

Variables Can Change

The value stored in a variable can be updated at any time:

score = 10
print(score)   # prints 10
score = 20
print(score)   # prints 20

The Assignment Operator

The = symbol is called the assignment operator. It assigns the value on the right to the variable on the left.

my_name = "Alice"
favourite_food = "pizza"

Quick Review

What is a variable?

A variable is a temporary storage of data in the computer’s memory. It is given an assigned name and holds a specific type of data.

What symbol is used to assign a value to a variable?

The = (equals) symbol, called the assignment operator.

Can the data stored in a variable change?

Yes. You can reassign a variable to a new value at any time.

Name one rule for variable names.

Any of: must start with a letter or underscore; cannot start with a number; can only contain alphanumeric characters and underscores; names are case-sensitive.