Variables and Expressions

A variable is a name for a quantity.

An expression is a recipe that uses quantities to produce another quantity.

That is the useful distinction:

variable: a name
expression: a computation

Variables Name Values

In

x=3x = 3

the symbol xx names the value 3.

The letter is not special. It is a handle. We use the handle because the value may appear in many places.

In ML, a variable may name:

  • a scalar such as a loss
  • a vector such as an embedding
  • a matrix such as a weight table
  • a probability
  • a model parameter

The principle is the same: the symbol lets us refer to a quantity without rewriting the quantity every time.

MATH-C01-T02-001Exercise: Read an assignment

In the statement a=7a = 7, what value does aa name?

Compute it first, then check your number.

HintRead the equals sign

Here the equals sign tells you what value the name refers to.

SolutionNamed value

The variable aa names the value 7. Here aa is a handle for a known quantity, not a mystery to solve.

Expressions Compute Values

This is an expression:

2x+12x + 1

It is a recipe:

take x
multiply by 2
add 1

If x=3x = 3, then:

2x+1=23+1=72x + 1 = 2 \cdot 3 + 1 = 7

If x=4x = 4, then:

2x+1=24+1=92x + 1 = 2 \cdot 4 + 1 = 9

Same expression. Different input. Different result.

MATH-C01-T02-002Exercise: Evaluate the expression

If x=5x = 5, what is 2x+12x + 1?

Compute it first, then check your number.

HintSubstitute first

Replace xx with 5, then follow the order of operations.

SolutionSubstitution

Substitute x=5x = 5:

2x+1=25+1=10+1=112x + 1 = 2 \cdot 5 + 1 = 10 + 1 = 11

This is the expression-as-recipe idea: replace the variable with its value, then follow the operations in order.

Constants and Variables

In

2x+12x + 1

the values 2 and 1 are constants. Their values are fixed inside this expression.

The value of xx can change.

This is why expressions are useful. One expression can describe many related computations.

MATH-C01-T02-003Exercise: Identify what changes

In 2x+12x + 1, enter 1 if xx is the part whose value can change.

Compute it first, then check your number.

HintCompare the symbols

Which symbol can be replaced by different input values?

SolutionReasoning

Enter 1. In this expression, xx is the variable. The numbers 2 and 1 are constants.

Code Mirror

The expression:

2x+12x + 1

can be mirrored in code:

x = 3
value = 2 * x + 1
print(value)

The code and the expression are doing the same work. The code makes each step explicit. The expression writes the computation compactly.

MATH-C01-T02-004Exercise: Trace the code

What does this code print?

x = 4
value = 2 * x + 1
print(value)

Compute it first, then check your number.

HintMirror the expression

This code evaluates 2x+12x + 1 with x=4x = 4.

SolutionTrace

The variable xx is assigned 4.

Then:

24+1=92 \cdot 4 + 1 = 9

So the code prints 9. The code line value = 2 * x + 1 is the same computation as the expression 2x+12x + 1 with x=4x = 4.

Common Doubt

Does a variable always stand for an unknown value?

No. Sometimes a variable is unknown. Sometimes it is known but named for convenience. In ML code, variables often name known arrays or tensors. The point is not mystery. The point is reference.

Next, we give expressions a name and treat them as functions.