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).
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)
When creating variable names (also called identifiers), you must follow these rules:
_)_)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"
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 = 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"
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.
The = (equals) symbol, called the assignment operator.
Yes. You can reassign a variable to a new value at any time.
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.