Please enable JavaScript to use CodeHS

Coding for High School Math

Coding for Algebra

Algebra is the language of patterns and unknowns. Code is the same language, just written for a computer. In this tutorial you will turn formulas into functions and watch a program solve equations step by step.

High School Math ~12 min Algebra 1 / 2
Variables

From variables to code

In algebra, a variable like x stands for a number we do not know yet. In programming, a variable is a named box that holds a value.

algebra.py
# Variables hold numbers
a = 2
b = 5
x = b / a
print("x =", x)
Output
x = 2.5

The computer follows the same rules you do. It divides b by a and stores the result in x. The big difference is that the computer never gets tired of doing the same steps over and over.

Expressions

Expressions and PEMDAS

An expression like 2x² + 3x - 7 tells a computer exactly what arithmetic to do. Code uses * for multiplication, ** for powers, and parentheses to control order.

Let x = 4. Then:

evaluate_expression.py
x = 4
y = 2 * x**2 + 3 * x - 7
print(y)
Output
37

The computer evaluates powers first, then multiplication, then addition and subtraction. Parentheses always win. That is the same PEMDAS order you learn in math class.

Linear Equations

Solving a linear equation

Take the equation ax + b = c. We want to find x. The algebra works like this:

  1. Subtract b from both sides: ax = c - b
  2. Divide both sides by a: x = (c - b) / a

In code, we turn those steps into a function. The function takes a, b, and c and returns the answer. Here is the plan:

solve_linear.py
def solve_linear(a, b, c):
    # ax + b = c
    right_side = c - b      # subtract b from both sides
    x = right_side / a   # divide both sides by a
    return x

print(solve_linear(2, 5, 13))  # solves 2x + 5 = 13
Output
4.0

Notice how each line of code matches a step in the algebra. The variable names right_side and x remind us what is happening. Good variable names are like showing your work.

Quadratic Formula

The quadratic formula step by step

The quadratic equation is ax² + bx + c = 0. The famous quadratic formula is:

x = (-b ± √(b² - 4ac)) / (2a)

Before we write code, let us break it into steps. The part under the square root is called the discriminant.

  1. Calculate the discriminant: D = b² - 4ac
  2. If D < 0, there are no real solutions.
  3. If D = 0, there is exactly one solution: x = -b / (2a)
  4. If D > 0, there are two solutions: x = (-b + √D) / (2a) and x = (-b - √D) / (2a)

Those steps are an algorithm. An algorithm is just a list of steps that always works, no matter what numbers you start with.

Interactive

Interactive quadratic solver

Enter values for a, b, and c. The tool will walk through the discriminant and the final answers one step at a time.


    
Code It

Quadratic formula in Python

Here is the same algorithm as a Python function. Try reading each line and matching it to the steps above.

solve_quadratic.py
import math

def solve_quadratic(a, b, c):
    # ax^2 + bx + c = 0
    discriminant = b**2 - 4 * a * c

    if discriminant < 0:
        return "No real solutions"

    root = math.sqrt(discriminant)
    denominator = 2 * a
    x1 = (-b + root) / denominator
    x2 = (-b - root) / denominator
    return (x1, x2)

print(solve_quadratic(1, -5, 6))
Output
(3.0, 2.0)

The function first checks the discriminant. If it is negative, it returns a message. Otherwise it computes the two roots. This is the power of code: one function handles every quadratic equation you throw at it.

Check Yourself

Quick quiz

1. In the quadratic formula, what does b² - 4ac tell us?

2. Which Python operator raises a number to a power?

3. If a computer solves 2x + 5 = 13, what is the correct first step?

Keep learning

Algebra is the foundation. Next, use code to explore shapes, distances, and angles in geometry.

Part of the “Coding for High School Math” series.