TExcelTAD
Home › Basic Math & Pre-Algebra › Variables and Expressions

Variables and Expressions

A variable is a letter that stands for a number you don't know yet, or for a number that can change. An expression combines numbers and variables with operations. This lesson is the first step from arithmetic toward algebra. If you have written x = 5 in a program, you already know half of it.

Letters that stand for numbers

In arithmetic every number is written out: 3×4+23 \times 4 + 2. In algebra we leave a slot open and give it a name:

3×n+23 \times n + 2

Here nn is a variable. Until we know nn, the expression is a recipe rather than a single number. For n=4n = 4 the recipe gives 3×4+2=143 \times 4 + 2 = 14. For n=10n = 10 it gives 32.

Algebra has a few writing habits worth learning early:

Written Means Why
3n3n 3×n3 \times n The multiplication sign is left out between a number and a letter
abab a×ba \times b Same rule between two letters
n2n^2 n×nn \times n Powers work as in the Exponents lesson
n/4n/4 or n÷4n \div 4 nn divided by 4 A fraction bar is also a division
−x-x −1×x-1 \times x The opposite of xx

Note: In code you must always write the *: 3*n, not 3n. Python reads 3n as a syntax error and ab as a single variable called ab.

Parts of an expression

Take 5x2−2x+75x^2 - 2x + 7:

  • Terms are the pieces joined by ++ and −-: 5x25x^2, −2x-2x, 77.
  • A coefficient is the number in front of a variable: 5 and −2.
  • A constant is a term with no variable: 7.
  • Like terms have exactly the same variable part: 4x4x and −9x-9x are like terms, while 4x4x and 4x24x^2 are not.

Evaluating an expression

To evaluate an expression you substitute a number for each variable and follow the order of operations. Put each substituted value in brackets so that negative numbers keep their sign:

5x2−2x+7with x=−3=5⋅(−3)2−2⋅(−3)+7=5⋅9+6+7=58\begin{aligned} &5x^2 - 2x + 7 && \text{with } x = -3 \\ &= 5 \cdot (-3)^2 - 2 \cdot (-3) + 7 \\ &= 5 \cdot 9 + 6 + 7 \\ &= 58 \end{aligned}

The same thing in Python, for several values at once:

python · try it yourself
def f(x):
    return 5 * x**2 - 2 * x + 7

for x in [-3, -1, 0, 1, 2.5]:
    print(f"x = {x:>4}:  5x² − 2x + 7 = {f(x)}")
x =   -3:  5x² − 2x + 7 = 58
x =   -1:  5x² − 2x + 7 = 14
x =    0:  5x² − 2x + 7 = 7
x =    1:  5x² − 2x + 7 = 10
x =  2.5:  5x² − 2x + 7 = 33.25

Simplifying: combining like terms and distributing

Two rules do most of the work.

Combine like terms by adding their coefficients: 4x+3−9x+10=(4−9)x+(3+10)=−5x+134x + 3 - 9x + 10 = (4 - 9)x + (3 + 10) = -5x + 13.

Distribute a factor over brackets (the distributive property from Properties of Operations): 3(2x−5)=6x−153(2x - 5) = 6x - 15, and −(x−4)=−x+4-(x - 4) = -x + 4.

2(3x+1)−4(x−2)=6x+2−4x+8distribute; −4⋅−2=+8=2x+10combine like terms\begin{aligned} &2(3x + 1) - 4(x - 2) \\ &= 6x + 2 - 4x + 8 && \text{distribute; } -4 \cdot -2 = +8 \\ &= 2x + 10 && \text{combine like terms} \end{aligned}

The SymPy library does symbolic algebra in Python, so it can check this kind of work. print shows SymPy expressions in Python syntax, so x**2 means x2x^2 and 2*x means 2x2x:

python · try it yourself
from sympy import symbols, expand, simplify

x, y = symbols("x y")
e1 = 2*(3*x + 1) - 4*(x - 2)
print(e1)
e2 = 4*x + 3 - 9*x + 10
print(e2)
print(expand((x + 3)*(x - 2)))
print(simplify((x**2 - 9)/(x - 3)))
print(e1.subs(x, 5))
2*x + 10
13 - 5*x
x**2 + x - 6
x + 3
20

SymPy simplified the first two expressions as soon as they were built, because combining like terms is automatic. It may also put the constant first (13 - 5*x), which is the same expression as −5x+13-5x + 13.

In software

Variables in programs and variables in algebra are close cousins, but not the same:

  • In algebra, xx stands for one fixed (possibly unknown) value throughout a problem. In code, x = x + 1 is an instruction to change it, which would be a contradiction as an equation.
  • Formulas in code are expressions: a shop's total price * qty * (1 + tax), a game's damage base * (1 + 0.1 * level), a CSS layout calc(100% - 2 * gap).
  • Spreadsheet cells are expressions whose variables are other cells.
  • Compilers simplify expressions too. Constant folding turns 60 * 60 * 24 into 86400 before the program runs.
python · try it yourself
def damage(base, level, crit):
    return base * (1 + 0.1 * level) * (2 if crit else 1)

print(damage(40, 0, False))
print(damage(40, 5, False))
print(damage(40, 5, True))
40.0
60.0
120.0

Exercises

Exercise 1: Evaluate

Evaluate 4a−3b+ab4a - 3b + ab for a=2a = 2 and b=−5b = -5.

Answer

13

Solution

Substitute with brackets, then follow the order of operations:

4⋅(2)−3⋅(−5)+(2)⋅(−5)=8+15−10−3⋅−5=+15, 2⋅−5=−10=13\begin{aligned} &4 \cdot (2) - 3 \cdot (-5) + (2) \cdot (-5) \\ &= 8 + 15 - 10 && -3 \cdot -5 = +15,\ 2 \cdot -5 = -10 \\ &= 13 \end{aligned}

A quick check in code:

python · try it yourself
a, b = 2, -5
print(4*a - 3*b + a*b)
13

Exercise 2: Combine like terms

Simplify 7y−2+3y2−4y+9−y27y - 2 + 3y^2 - 4y + 9 - y^2.

Answer

2y2+3y+72y^2 + 3y + 7

Solution

Group the terms by their variable part:

y2 terms:3y2−y2=2y2y terms:7y−4y=3yconstants:−2+9=7result:2y2+3y+7\begin{aligned} y^2\ \text{terms:} &\quad 3y^2 - y^2 = 2y^2 \\ y\ \text{terms:} &\quad 7y - 4y = 3y \\ \text{constants:} &\quad -2 + 9 = 7 \\ \text{result:} &\quad 2y^2 + 3y + 7 \end{aligned}

Exercise 3: Distribute and simplify

Simplify 5(x−2)−3(2x−4)5(x - 2) - 3(2x - 4).

Answer

−x+2-x + 2

Solution
5(x−2)−3(2x−4)=5x−10−6x+12−3⋅−4=+12=−x+2\begin{aligned} &5(x - 2) - 3(2x - 4) \\ &= 5x - 10 - 6x + 12 && -3 \cdot -4 = +12 \\ &= -x + 2 \end{aligned}
python · try it yourself
from sympy import symbols, expand
x = symbols("x")
print(expand(5*(x - 2) - 3*(2*x - 4)))
2 - x

Exercise 4: Write the expression

A taxi charges €3.50 to start plus €1.80 per kilometre. Write an expression for the fare of a kk-kilometre ride, then write a Python function and use it for 12 km.

Answer

3.5+1.8k3.5 + 1.8k; a 12 km ride costs €25.10.

Solution

The fixed part is the constant, the per-kilometre price is the coefficient of kk: fare=3.5+1.8k\text{fare} = 3.5 + 1.8k. For k=12k = 12: 3.5+21.6=25.13.5 + 21.6 = 25.1.

python · try it yourself
def fare(k):
    return 3.5 + 1.8 * k

print(f"{fare(12):.2f}")
25.10

Test yourself

What is the coefficient of xx in 7−4x+x27 - 4x + x^2?

  1. 7
  2. 4
  3. −4
  4. 1

Which expression equals 3(x−2)−(x+1)3(x - 2) - (x + 1)?

  1. 2x−72x - 7
  2. 2x−52x - 5
  3. 4x−74x - 7
  4. 2x−12x - 1

Why does Python reject y = 3x + 1?

  1. Python has no variables called x
  2. Multiplication must be written with *, as 3*x
  3. Python cannot add 1 to a variable
  4. The = sign must be ==