Module 3 · Beginner · Algebra

Inequalities explained for beginners

Inequalities: the symbols, number lines, solving step by step, the flip rule, intervals, and filtering data with conditions in pandas.

By theprogrammers team · · 8 min read

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 True or False for each value.

The four symbols

SymbolMeaningExampleIn words
<less thanx < 30x is below 30
>greater thanx > 10,000x is above 10,000
≤less than or equal tox ≤ 5x is at most 5
≥greater than or equal tox ≥ 18x 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

WordsInequality
at least 18x ≥ 18
at most 5x ≤ 5
more than 100x > 100
fewer than 3x < 3
no more than 40x ≤ 40
no less than 60x ≥ 60
between 18 and 65, inclusive18 ≤ 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()
Three number lines from −6 to 6: the first shades from a filled dot at 2 to the right edge for x ≥ 2; the second shades from the left edge to an open circle at −1 for x < −1; the third shades between an open circle at −3 and a filled dot at 4 for −3 < x ≤ 4.

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

  1. Subtract 5: 3x > 15.
  2. 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

  1. Expand: 2x − 8 ≤ 10.
  2. Add 8: 2x ≤ 18.
  3. 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

  1. Subtract 10: −2x ≥ −6.
  2. 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:

  1. Subtract 1 from each part: −4 < 2x ≤ 8.
  2. 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:

InequalityInterval notationMeaning
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

  1. 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

  1. Solve 4x − 7 < 13.
Show the answer

4x < 20, so x < 5.

  1. Solve 5 − 3x ≥ 20.
Show the answer

−3x ≥ 15. Dividing by −3 flips the sign: x ≤ −5. Check x = −6: 5 + 18 = 23 ≥ 20 ✓.

  1. Solve 1 ≤ (x + 3)/2 < 6.
Show the answer

Multiply all parts by 2: 2 ≤ x + 3 < 12. Subtract 3: −1 ≤ x < 9.

  1. 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

  1. Find values that satisfy two equations at once in solve two equations at once.
  2. Revisit rearrange formulas.