Inequalities
An inequality compares two expressions that are not necessarily equal: one side is smaller, larger, or "at most" or "at least" the other. You solve it almost exactly like an equation, with one extra rule about negative numbers. The answer is usually a whole range of numbers rather than a single value.
The four symbols
| Symbol | Read as | Example true statement |
|---|---|---|
| less than | ||
| greater than | ||
| less than or equal to | ||
| greater than or equal to |
The small end of points at the smaller number. In code these are <, >, <= and >=.
A solution such as contains infinitely many numbers: 2, 0, −100 and 2.999 all work. On a number line it is drawn as a ray. An open circle at 3 means 3 itself is excluded (, ), and a filled circle means it is included (, ):
x < 3 ←———————————○ (3 not included)
x ≥ −2 ●———————————→ (−2 included)
−3 −2 −1 0 1 2 3 4Solving: same moves, one twist
You may add or subtract the same number on both sides, and multiply or divide by a positive number, without changing the direction:
Multiplying or dividing by a negative number reverses the inequality. Look at a true statement: . Multiply both sides by −1 and you get −2 and −5. On the number line −2 is to the right of −5, so the true statement is now .
Tip: To check an inequality, test one number inside your answer and one outside. The inside number must make the original inequality true and the outside number must make it false.
Compound inequalities
means two conditions at once. Do each step to all three parts:
Python supports exactly this chained style, so you can test the answer for a list of numbers:
def ok(x):
return -1 <= 2*x + 3 < 9
for x in [-3, -2, 0, 2.5, 3, 4]:
print(f"x = {x:>4}: {ok(x)}")x = -3: False
x = -2: True
x = 0: True
x = 2.5: True
x = 3: False
x = 4: False
Checking with SymPy
SymPy's solve also handles inequalities in one variable. It prints the answer as a logical statement: & means "and", and oo is SymPy's spelling of infinity ().
from sympy import symbols, solve
x = symbols("x", real=True)
print(solve(2*x + 5 < 17, x))
print(solve(-3*x + 4 >= 19, x))
print(solve([2*x + 3 >= -1, 2*x + 3 < 9], x))x < 6
x <= -5
(-2 <= x) & (x < 3)
In software
Inequalities are everywhere code makes a decision:
- Bounds checks. A valid list index satisfies . Most off-by-one bugs come from mixing up and at the end of a range.
- Clamping.
max(lo, min(hi, v))forces , for a volume slider or a camera angle. - Budgets and limits. "Show free shipping if ", "rate-limit when per minute".
- Collision tests. Two boxes overlap when four inequalities hold at once (see AABB collision).
Solving an inequality tells you the exact threshold. For example, how many €7.50 items fit a €60 budget after a €4.25 delivery fee?
import math
def clamp(v, lo, hi):
return max(lo, min(hi, v))
print([clamp(v, 0, 100) for v in [-20, 35, 140]])
# 7.5n + 4.25 <= 60 -> n <= (60 - 4.25) / 7.5
limit = (60 - 4.25) / 7.5
print(round(limit, 3), "-> at most", math.floor(limit), "items")[0, 35, 100]
7.433 -> at most 7 items
Because must be a whole number, the largest allowed value is the floor of 7.433, which is 7.
Exercises
Exercise 1: One step with a flip
Solve and give one number that satisfies it and one that doesn't.
Answer
(for example 0 works, −6 does not).
Solution
Exercise 2: Variables on both sides
Solve .
Answer
Solution
Alternatively, move the terms to the right side () and you never divide by a negative: , which is the same answer.
Exercise 3: Compound
Solve .
Answer
Solution
print([x for x in range(-8, 2) if 4 < 3 - x <= 9])[-6, -5, -4, -3, -2]
Exercise 4: Game level threshold
A character needs at least 1000 XP for the next level. They have 340 XP and earn 45 XP per quest. Write and solve an inequality for the number of quests , then compute it in Python with math.ceil.
Answer
gives , so 15 quests.
Solution
must be a whole number, so we round up: 14 quests give 970 XP (not enough), 15 give 1015.
import math
q = math.ceil((1000 - 340) / 45)
print(q, 340 + 45 * (q - 1), 340 + 45 * q)15 970 1015
Test yourself
Solve .
Which condition describes a valid index for a Python list of length ?
On a number line, how do you draw ?
- Open circle at 2, ray to the left
- Filled circle at 2, ray to the right
- Open circle at 2, ray to the right
- Filled circle at 2, ray to the left