Sub topic 5
Computing slope from two points
This chapter assumes you can plot points on the coordinate plane, work with signed numbers and fractions, and rearrange simple expressions. You should also know that slope is rise over run and that its sign tells you whether a line rises, falls, is flat, or is vertical. Here you turn that idea into a formula that works from any two points, with no graph needed.
From a graph to a formula
On a graph you can count squares: go up the rise, then across the run. That approach breaks down when the points are far apart, have fractional coordinates, or are given only as numbers. The slope formula does the same counting with subtraction.
Take two points, written as ordered pairs: (x₁, y₁) and (x₂, y₂). The subscripts 1 and 2 only label which point is which.
- The rise is the change in y: y₂ − y₁.
- The run is the change in x: x₂ − x₁.
Dividing rise by run gives the slope:
m = (y₂ − y₁) / (x₂ − x₁)
Here m is the standard letter for slope. The expression is often written as Δy / Δx, where the Greek letter delta (Δ) means "change in".
A worked example
Find the slope of the line through (1, 2) and (3, 6).
- Label the points: (x₁, y₁) = (1, 2) and (x₂, y₂) = (3, 6).
- Subtract the y-values: 6 − 2 = 4.
- Subtract the x-values in the same order: 3 − 1 = 2.
- Divide: m = 4 / 2 = 2.
The slope is 2. For every 1 unit you move right, the line rises 2 units.
The order rule
You may call either point "point 1". Both orders give the same slope, as long as you use the same order in the numerator and the denominator.
Using the same points reversed, (x₁, y₁) = (3, 6) and (x₂, y₂) = (1, 2):
m = (2 − 6) / (1 − 3) = (−4) / (−2) = 2
Both the rise and the run changed sign, so the quotient is unchanged. The common mistake is mixing the orders, for example (y₂ − y₁) / (x₁ − x₂). That gives −2, which has the wrong sign.
Negative numbers and fractions
Most errors come from subtracting negative numbers. Subtracting a negative is the same as adding its opposite, so write the subtraction with parentheses before you simplify.
Find the slope through (−2, 5) and (4, −1).
- Rise: −1 − 5 = −6.
- Run: 4 − (−2) = 4 + 2 = 6.
- Slope: −6 / 6 = −1.
The slope is negative, so the line falls as you move right.
A slope does not have to be a whole number. Through (0, 1) and (3, 3):
m = (3 − 1) / (3 − 0) = 2/3
Leave the slope as a fraction in lowest terms. The fraction 2/3 reads directly as "rise 2, run 3", which is convenient for graphing later. A decimal such as 0.6667 loses that information and is only an approximation.
Checking the sign against the picture
Before you accept an answer, compare its sign with the points. If the second point is to the right and higher, the slope must be positive. If it is to the right and lower, the slope must be negative.
| Situation | Rise | Run | Slope |
|---|---|---|---|
| Line goes up to the right | positive | positive | positive |
| Line goes down to the right | negative | positive | negative |
| Horizontal line | 0 | nonzero | 0 |
| Vertical line | nonzero | 0 | undefined |
This check is quick and catches most sign errors.
The two special cases
Horizontal lines: slope 0
Both points have the same y-value. Through (−3, 4) and (5, 4):
m = (4 − 4) / (5 − (−3)) = 0 / 8 = 0
The rise is 0, so the slope is 0. Zero divided by a nonzero number is zero. The line is y = 4.
Vertical lines: undefined slope
Both points have the same x-value. Through (2, 1) and (2, 7):
m = (7 − 1) / (2 − 2) = 6 / 0
Division by zero has no value, so the slope is undefined. This is different from a slope of 0, and the two are easy to confuse. A horizontal line has slope 0. A vertical line has no slope. The line is x = 2, and it cannot be written as y = mx + b, because no single number m fits.
Computing slope in code
A function makes the rules explicit, including the vertical-line case. It checks for equal x-values first, so it never divides by zero.
def slope(x1, y1, x2, y2):
if x1 == x2:
raise ValueError("A vertical line has undefined slope")
return (y2 - y1) / (x2 - x1)
print(slope(1, 2, 3, 6)) # 2.0
print(slope(0, 5, 4, 1)) # -1.0The first call matches the worked example. The second uses (0, 5) and (4, 1): rise 1 − 5 = −4, run 4 − 0 = 4, slope −1.
Python's / operator returns a float. For exact fractions, use the fractions module so that 2/3 stays 2/3:
from fractions import Fraction
def exact_slope(x1, y1, x2, y2):
if x1 == x2:
raise ValueError("A vertical line has undefined slope")
return Fraction(y2 - y1, x2 - x1)
print(exact_slope(0, 1, 3, 3)) # 2/3
print(exact_slope(-2, 5, 4, -1)) # -1Floating-point numbers can introduce small rounding errors. For example, a slope of 1/3 is stored as 0.3333333333333333, which you cannot multiply back to get exactly 1. Using Fraction avoids this when the inputs are integers.
Why any two points work
A line has the same slope everywhere. That is what makes it a line. So you can pick any two points on it and get the same answer.
Check with the line y = 2x + 1. The points (0, 1), (1, 3), and (4, 9) all lie on it.
- From (0, 1) to (1, 3): 2 / 1 = 2.
- From (1, 3) to (4, 9): 6 / 3 = 2.
- From (0, 1) to (4, 9): 8 / 4 = 2.
This consistency also gives a test. If three points do not produce the same slope between each pair, they do not lie on one line.
Common errors
| Error | Example | Fix |
|---|---|---|
| Mixed subtraction order | (y₂ − y₁) / (x₁ − x₂) | Use the same point order on top and bottom |
| Run over rise | Dividing x-change by y-change | Rise is on top, run is on the bottom |
| Sign slip with negatives | 4 − (−2) computed as 2 | Rewrite as 4 + 2 = 6 |
| Subtracting x from y | y₂ − x₂ | Subtract y from y, and x from x |
| Calling a vertical line slope 0 | 6 / 0 treated as 0 | Division by zero is undefined |
Using the result
Once you have a slope, you are one step from an equation. The slope is the m in y = mx + b. In the next chapters you will find b, the y-intercept, and use it to write the full equation. The function line_through in a later chapter builds on this one by calling the same formula and then solving for b.
Exercises
Exercise
Find the slope of the line through (−3, 4) and (2, −6). Is the line rising or falling?
Show answer
Rise = −6 − 4 = −10. Run = 2 − (−3) = 5. Slope = −10 / 5 = −2. The slope is negative, so the line falls from left to right.
Exercise
A student computes the slope through (1, 5) and (4, 11) as (11 − 5) / (1 − 4) and gets −2. What went wrong, and what is the correct slope?
Show answer
The student used y₂ − y₁ on top but x₁ − x₂ on the bottom, mixing the orders. The correct calculation is (11 − 5) / (4 − 1) = 6 / 3 = 2.
Exercise
Find the slope through (5, −2) and (5, 9), and through (−1, 3) and (6, 3). Describe each line.
Show answer
First pair: the x-values are equal, so the run is 0 and the slope 11 / 0 is undefined. The line is vertical, x = 5. Second pair: the y-values are equal, so the rise is 0 and the slope is 0 / 7 = 0. The line is horizontal, y = 3.
Exercise
Do the points (0, 1), (2, 5), and (5, 11) lie on one line?
Show answer
Slope from (0, 1) to (2, 5) is 4 / 2 = 2. Slope from (2, 5) to (5, 11) is 6 / 3 = 2. The slopes match, so the points lie on one line (y = 2x + 1).