Real-life rules are rarely "exactly equal". You must be at least 18 to vote. A parcel must weigh less than 30 kg. A model is good enough if its error is at most 5%. A customer is "high value" if they spend more than ₹10,000. These are inequalities: statements that one thing is bigger or smaller than another.
Inequalities matter in data science because filtering data is writing inequalities. Every time you select rows where age >= 18 or price < 500, you're using one. This lesson explains the symbols, how to show inequalities on a number line, how to solve them (with the one rule that's different from equations), how to combine conditions, and how all of it turns into pandas filters.
What you need first: solve linear equations step by step and negative numbers.
Key terms
- Inequality: a statement that two values are not equal, using <, >, ≤ or ≥.
- Solution set: all the values that make an inequality true. Unlike an equation, there are usually infinitely many.
- Interval: a continuous range of numbers, like "between 18 and 65".
- Boundary: the number where the inequality switches between true and false.
- Condition: in code, an inequality that is either
TrueorFalsefor each value.
The four symbols
| Symbol | Meaning | Example | In words |
|---|---|---|---|
| < | less than | x < 30 | x is below 30 |
| > | greater than | x > 10,000 | x is above 10,000 |
| ≤ | less than or equal to | x ≤ 5 | x is at most 5 |
| ≥ | greater than or equal to | x ≥ 18 | x is at least 18 |
A memory trick: the symbol is a mouth that opens towards the bigger number. In 3 < 8, it opens towards 8.
The line under ≤ and ≥ means "or equal to". The difference matters at the boundary: someone exactly 18 can vote under x ≥ 18, but couldn't under x > 18.
Translating words
| Words | Inequality |
|---|---|
| at least 18 | x ≥ 18 |
| at most 5 | x ≤ 5 |
| more than 100 | x > 100 |
| fewer than 3 | x < 3 |
| no more than 40 | x ≤ 40 |
| no less than 60 | x ≥ 60 |
| between 18 and 65, inclusive | 18 ≤ x ≤ 65 |
Showing inequalities on a number line
On a number line, an inequality is a ray or a segment:
- A filled dot (●) means the boundary is included (≤ or ≥).
- An open circle (○) means it isn't (< or >).
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3, 1, figsize=(8, 3.6))
cases = [("x ≥ 2", 2, None, True, None), ("x < −1", None, -1, None, False),
("−3 < x ≤ 4", -3, 4, False, True)]
for ax, (label, low, high, low_closed, high_closed) in zip(axes, cases):
ax.axhline(0, color="black", lw=1)
start = low if low is not None else -6
end = high if high is not None else 6
ax.plot([start, end], [0, 0], color="tab:blue", lw=5)
if low is not None:
ax.plot(low, 0, "o", ms=10, color="tab:blue", mfc="tab:blue" if low_closed else "white")
if high is not None:
ax.plot(high, 0, "o", ms=10, color="tab:blue", mfc="tab:blue" if high_closed else "white")
ax.set_xlim(-6, 6)
ax.set_xticks(range(-6, 7))
ax.set_yticks([])
ax.set_title(label, loc="left")
for side in ["left", "right", "top"]:
ax.spines[side].set_visible(False)
plt.tight_layout()
plt.show()
Solving inequalities
Solving an inequality works almost exactly like solving an equation: do the same thing to both sides until the variable is alone.
3x + 5 > 20
- Subtract 5: 3x > 15.
- Divide by 3: x > 5.
Any number above 5 works: check x = 6: 18 + 5 = 23 > 20 ✓. And the boundary: x = 5 gives 20 > 20, which is false, as expected for a strict >.
2(x − 4) ≤ 10
- Expand: 2x − 8 ≤ 10.
- Add 8: 2x ≤ 18.
- Divide by 2: x ≤ 9.
The one new rule: flip when multiplying or dividing by a negative
Start with something true: 2 < 5. Multiply both sides by −1: −2 and −5. But −2 is bigger than −5. So the true statement is −2 > −5: the symbol had to flip.
Multiplying or dividing by a negative number reverses the order of numbers on the number line, so the inequality sign must flip.
10 − 2x ≥ 4
- Subtract 10: −2x ≥ −6.
- Divide by −2, and flip the sign: x ≤ 3.
Check: x = 0 gives 10 ≥ 4 ✓; x = 5 gives 0 ≥ 4 ✗. So the answer x ≤ 3 is right. Forgetting the flip would give x ≥ 3, which fails the check.
Adding or subtracting negatives doesn't flip anything. Only multiplying or dividing by a negative does.
Double inequalities
−3 < 2x + 1 ≤ 9 describes a range. Do the same thing to all three parts:
- Subtract 1 from each part: −4 < 2x ≤ 8.
- Divide each part by 2: −2 < x ≤ 4.
Combining conditions: and, or
- AND: both must be true. "Age at least 18 and under 65" is 18 ≤ x < 65: a single segment.
- OR: at least one must be true. "Under 5 or over 60" is x < 5 or x > 60: two separate rays.
Interval notation
Mathematicians often write ranges with brackets:
| Inequality | Interval notation | Meaning |
|---|---|---|
| 2 ≤ x ≤ 5 | [2, 5] | square brackets include the ends |
| 2 < x < 5 | (2, 5) | round brackets exclude them |
| 2 ≤ x < 5 | [2, 5) | mixed |
| x ≥ 2 | [2, ∞) | infinity always gets a round bracket |
You'll see this in pandas: when you put values into bins, the bin labels use exactly this notation, like (18, 35].
Inequalities in data science
Filtering rows
In Python, an inequality is a condition that evaluates to True or False. In pandas, applying it to a column gives True or False for every row, and you use that to filter:
import pandas as pd
customers = pd.DataFrame({
"name": ["Asha", "Ben", "Chen", "Divya", "Eli", "Farah"],
"age": [17, 25, 42, 66, 35, 19],
"spend": [1200, 15000, 8000, 22000, 9500, 400],
})
print(customers[customers["age"] >= 18])
print(customers[(customers["age"] >= 18) & (customers["age"] < 65)]["name"].tolist())
print(customers[(customers["spend"] > 10000) | (customers["age"] < 18)]["name"].tolist())
print(customers[customers["spend"].between(5000, 10000)]["name"].tolist())
name age spend
1 Ben 25 15000
2 Chen 42 8000
3 Divya 66 22000
4 Eli 35 9500
5 Farah 19 400
['Ben', 'Chen', 'Eli', 'Farah']
['Asha', 'Ben', 'Divya']
['Chen', 'Eli']
&means AND,|means OR. Each condition needs its own brackets.between(5000, 10000)includes both ends: 5000 ≤ spend ≤ 10000. It's the double inequality [5000, 10000].- Selecting and filtering data in pandas covers this in depth.
Counting with conditions
Because True counts as 1 and False as 0, summing a condition counts the rows where it's true, and averaging gives the proportion:
print((customers["spend"] > 10000).sum())
print((customers["spend"] > 10000).mean())
2
0.3333333333333333
Two of six customers, a third, spend more than ₹10,000.
Decision thresholds
A classifier turns a probability into a decision with an inequality: predict spam if p ≥ 0.5. Changing the threshold changes the trade-off between catching spam and false alarms, as explained in classification metrics. Decision trees are built entirely from inequalities like "age ≤ 3.5", as you'll see in decision trees explained.
Outlier rules
A common rule flags values outside Q1 − 1.5 × IQR ≤ x ≤ Q3 + 1.5 × IQR as outliers: a double inequality. Detect and treat outliers uses it.
Practice
- Write as an inequality: (a) at least 21 (b) less than ₹500 (c) between 10 and 20, including 10 but not 20.
Show the answer
(a) x ≥ 21 (b) x < 500 (c) 10 ≤ x < 20
- Solve 4x − 7 < 13.
Show the answer
4x < 20, so x < 5.
- Solve 5 − 3x ≥ 20.
Show the answer
−3x ≥ 15. Dividing by −3 flips the sign: x ≤ −5. Check x = −6: 5 + 18 = 23 ≥ 20 ✓.
- Solve 1 ≤ (x + 3)/2 < 6.
Show the answer
Multiply all parts by 2: 2 ≤ x + 3 < 12. Subtract 3: −1 ≤ x < 9.
- Write a pandas filter for customers aged under 30 who spend at least ₹5,000.
Show the answer
customers[(customers["age"] < 30) & (customers["spend"] >= 5000)]. In the example data, that's just Ben.
Key takeaways
- < and > are strict; ≤ and ≥ include the boundary (filled dot on a number line).
- Solve inequalities like equations, except: multiplying or dividing by a negative flips the sign.
- Double inequalities describe ranges; do the same thing to all three parts.
- AND narrows a selection; OR widens it.
- In pandas, inequalities become filters: use
&for and,|for or, and brackets around each condition.
Frequently asked questions
Why does the inequality sign flip when dividing by a negative number?
Multiplying or dividing by a negative reverses the order of numbers on the number line. Since 2 < 5 but −2 > −5, the sign must flip to keep the statement true.
What is the difference between > and ≥?
means strictly greater than, so the boundary value is excluded. ≥ means greater than or equal to, so the boundary is included. x > 18 excludes 18; x ≥ 18 includes it.
How do I filter rows with two conditions in pandas?
Wrap each condition in brackets and combine them with & for and, or | for or, such as df[(df["age"] >= 18) & (df["age"] < 65)].
What to learn next
- Find values that satisfy two equations at once in solve two equations at once.
- Revisit rearrange formulas.