What Is a Variable?
In programming, a variable is a named place in memory that stores a value your program can read and change while it runs. The name, such as score or user_name, lets the code refer to the value without knowing where it is stored.
Updated September 24, 2026
A game that keeps score needs to remember a number, add to it, and show it at the end. The number cannot be written into the code, because it is different every time someone plays. So the program sets aside a spot in memory, gives it a name like score, and uses that name whenever it needs the current value.
How a variable works
Three things happen to a variable during a program:
- Creation. The program reserves memory for a value and attaches a name to it. In Python this happens the first time you assign to the name. In Java and C you declare the variable first, with a type, as in
int score;. - Assignment. A value is stored under the name with
=. Assigning again replaces the old value. - Use. Wherever the name appears in an expression, the program reads the value it holds at that moment.
Start: 0
After one point: 10
Doubled: 20
On each assignment line, the right side is worked out first, using the value score has at that moment. Then the result is stored back under the same name. The name stays the same while the value it holds varies.
The equals sign means "store"
In math, x = x + 1 has no solution. In a program it is an ordinary instruction: take the current value of x, add 1, and store the result in x. The = sign is the assignment operator, and it always moves a value from the right side into the name on the left.
To ask whether two values are equal, programming languages use a different operator, ==. The expression x == 5 does not change anything; it produces True or False, a boolean.
The math meaning is related but not the same. In algebra, the x in 2x + 3 = 7 stands for one unknown number that you solve for. In a program, a variable holds a known value that can change as the program runs.
Variables and data types
Every value has a type: a whole number, a decimal number, a piece of text, a boolean, a list. Languages differ in whether the variable itself has a type.
In Python and JavaScript, the value carries the type, and one variable can hold values of different types over time. This is called dynamic typing:
42 <class 'int'>
forty-two <class 'str'>
In Java, C and C++, the variable has a fixed type chosen when you declare it. This is static typing: the compiler rejects code that tries to store text in an int variable, before the program ever runs.
int age = 30; // Java: type, name, value
String name = "Rosa";
let age = 30; // JavaScript: can be reassigned
const PI = 3.14159; // cannot be reassigned
In C, the type also decides how much memory the variable takes. An int is 4 bytes on almost every current system, a double is 8, and a char is exactly 1. To keep many values under one name, such as all the scores in a game, you use an array or a list and reach each value by its position, as in scores[0].
Boxes and labels
A common picture of a variable is a box that holds a value. That picture fits C well: int x; really is a fixed area of memory, and y = x; copies the value into a second area.
Python works more like labels. Every value is an object, and a variable is a name attached to one. The difference shows when two names refer to the same list:
5 6
[1, 2, 3, 4]
With the numbers, y = y + 1 creates a new number and moves the label y to it, so x still refers to 5. With the list, b = a copies nothing: both names refer to one list, so a change made through b shows up through a. Write b = a.copy() when you want a separate list. Objects and arrays in Java and JavaScript behave the same way.
Scope: where a variable exists
A variable is visible only in part of a program, called its scope. A variable created inside a function is local: it exists while the function runs and is gone when the function returns. A variable created at the top level of a file is global and can be read anywhere in that file.
Local variables are easier to reason about, because every line that can change them is inside one function. Programs that rely on many global variables are harder to debug for the opposite reason.
Naming variables
Good names say what the value means: total_price, is_logged_in, retries_left. Most languages share the same rules:
- a name is made of letters, digits and underscores, and cannot start with a digit;
- a name cannot be a keyword of the language, such as
if,fororclass; - names are case sensitive, so
totalandTotalare two different variables.
Style differs by language. Python uses snake_case (user_name), while Java and JavaScript use camelCase (userName). Values that should never change are often named in capitals, as in MAX_SIZE.
Common mistakes
Using a variable before it has a value. Python stops with a NameError:
NameError: name 'total' is not defined
The same error appears when a name is misspelled, because totla and total are different names to the computer.
Writing = where you mean ==. In Python, if x = 5: is a syntax error, so you find out at once. In C and JavaScript, if (x = 5) is valid code: it stores 5 in x, and the condition is always true. The program runs and gives wrong results, a logic error that can take a long time to find.
Giving one variable two jobs. A variable that holds a price at the top of a function and a count at the bottom still works, but every reader has to track which meaning is current. Give each meaning its own name.
Where to go next
Variables hold values, and the next step is knowing what kinds of values there are: read what a boolean is for true and false, and what an array is for many values under one name. The Python variables guide covers assignment, naming rules and multiple assignment in more detail, and the Python course has you write and change variables from the first lesson.
Frequently Asked Questions
What is a variable in Python?
age = 30, with no type declaration, and the same name can later refer to a value of a different type. By convention, Python variable names are written in snake_case.What is a simple definition of a variable?
How do you explain a variable to a kid?
score, and the box holds one thing at a time. You can look inside to see what is there, or take it out and put something new in, and the label stays the same.What is the difference between a variable and a constant?
final, and JavaScript and C use const. Python has no enforced constants, so programmers write the name in capitals, like MAX_USERS, to signal that it should not change.