Learning path

Full curriculum

Full curriculum

Unit content

Variables and values

Programs work with values such as numbers, text or Boolean states. A variable gives a name to a value so that the program can refer to it later.

For example,

price = 12
quantity = 3

binds the names price and quantity to values.

Assignment changes state

An assignment updates the program's state. After

count = 4
count = count + 1

count has value 5.

The second line is not a mathematical equation. It evaluates the expression on the right using the old value and then stores the result under the name on the left.

Names and values are different

Several variables may hold equal values without being the same variable. A name is part of the program's representation; the value is the data associated with that name at a particular moment.

Constants and mutability

Some languages allow names whose binding cannot be reassigned, while others distinguish between rebinding a name and mutating the object to which it refers.

The central idea is state: variables let a program remember intermediate results and let later operations depend on earlier computations.