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: . In algebra we leave a slot open and give it a name:
Here is a variable. Until we know , the expression is a recipe rather than a single number. For the recipe gives . For it gives 32.
Algebra has a few writing habits worth learning early:
| Written | Means | Why |
|---|---|---|
| The multiplication sign is left out between a number and a letter | ||
| Same rule between two letters | ||
| Powers work as in the Exponents lesson | ||
| or | divided by 4 | A fraction bar is also a division |
| The opposite of |
Note: In code you must always write the
*:3*n, not3n. Python reads3nas a syntax error andabas a single variable calledab.
Parts of an expression
Take :
- Terms are the pieces joined by and : , , .
- 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: and are like terms, while and 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:
The same thing in Python, for several values at once:
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: .
Distribute a factor over brackets (the distributive property from Properties of Operations): , and .
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 and 2*x means :
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 .
In software
Variables in programs and variables in algebra are close cousins, but not the same:
- In algebra, stands for one fixed (possibly unknown) value throughout a problem. In code,
x = x + 1is 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 damagebase * (1 + 0.1 * level), a CSS layoutcalc(100% - 2 * gap). - Spreadsheet cells are expressions whose variables are other cells.
- Compilers simplify expressions too. Constant folding turns
60 * 60 * 24into86400before the program runs.
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 for and .
Answer
13
Solution
Substitute with brackets, then follow the order of operations:
A quick check in code:
a, b = 2, -5
print(4*a - 3*b + a*b)13
Exercise 2: Combine like terms
Simplify .
Answer
Solution
Group the terms by their variable part:
Exercise 3: Distribute and simplify
Simplify .
Answer
Solution
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 -kilometre ride, then write a Python function and use it for 12 km.
Answer
; a 12 km ride costs €25.10.
Solution
The fixed part is the constant, the per-kilometre price is the coefficient of : . For : .
def fare(k):
return 3.5 + 1.8 * k
print(f"{fare(12):.2f}")25.10
Test yourself
What is the coefficient of in ?
- 7
- 4
- −4
- 1
Which expression equals ?
Why does Python reject y = 3x + 1?
- Python has no variables called x
- Multiplication must be written with
*, as3*x - Python cannot add 1 to a variable
- The = sign must be ==