Mario Sanchez Carrion profile picture

Mario Sanchez Carrion

I am a math tutor currently helping middle and high school students master common math concepts, and getting them prepared for the SAT and ACT tests.

I am also a computer programming enthusiast, especially interested in data analytics using Python, SQL and R.

I previously worked in international marketing and product development for Stanley Black & Decker, for more than 25 years.

👋 Contact me at mario@mariosanchez.org.

Solving Equations with Python

Using Python's sympy library

Important: This article assumes that you are already familiar with the basics of programming with Python.


In high school algebra you will regularly need to solve linear equations, quadriatic equations, or equations of a higher power, as well as systems of equations.

While it is important for you to learn how to solve them algebraically, you can also use the power of computers, and in particular Python (a high-level, general-purpose programming language) to solve the most complex equations in a matter of miliseconds.

To solve equations with Python, you need to install a library called sympy, which handles all the behind-the-scenes “magic” that generates the answers.

To install sympy (or any Python package) system-wide, you need to run the following command from the command prompt: sudo apt install python3-xyz, where xyz is the name of the package you are trying to install. In this case we will run:

sudo apt install python3-sympy

We will be using sympy to create two extremely brief snippets (just two lines of code each!). One of them will allow us to solve equations with one variable, and the other one systems of equations. The equations can be linear, quadratic, cubic, etc. and the solutions can be real or complex numbers.

Important note: for the programs to work, the equations must be input in Python format: for example, x to the power of two is x**2, the square root of 4 is sqrt(4), and 5 times x is 5*x.

Solving equations with one variable

This is the code to solve equations with one variable (in this case, we are solving a quadratic equation: x**2 - 5*x + 6). You can cut and paste this code into a Python .py file to be able to run it:

import sympy
from sympy.abc import x
from sympy.solvers import solve


# Put the equation here:
eq = (x**2 - 5*x + 6)

print("x = ", solve(eq, x))

The solution will be presented in the following form:

x = [2, 3]

Solving systems of equations

This is the code to solve systems of equations (in this case we want to find the solution of a pair of linear equations: x + y = 2 and x = y). For the program to work, we should write our expressions putting all our variables and costants together, like this: x + y - 2 and x - y. Again, you can cut and paste this code into a Python .py file to be able to run it:

import sympy
from sympy.abc import x, y
from sympy.solvers import solve


# Put the equations here:
eq = ([x + y - 2, x - y])

print("x = ", solve(eq, [x, y]))

The solution will be presented in the following form:

x = {x: 1, y: 1}

To run the programs you need to type the following command: python3 <snippet.py> replacing snippet with the name you assigned to your particular program.

While the equations used in this example are very simple, the programs also work for more complicated expressions.

This is just one example of how you can leverage the power of computers to solve equations quickly and reliably.

Last updated Jan 5, 2024

© 2025 Mario Sanchez Carrion