Linear Equations

Linear equations are a basic form of mathematical equations that occur in many different applications. They describe a relationship between variables that are linearly connected to each other. A linear equation usually takes the form:

ax + b = c

Here are the main components of a linear equation:

  1. Variables (‚x‘): This is the unknown value you want to find out. It is the variable that is sought in the equation.
  2. Coefficients (‚a‘ and ‚b‘): These are well-known numbers that describe the linear relationship between the variable ‚x‘ and the constant value ‚c‘.
  • The coefficient ‚a‘ is the coefficient of the variable ‚x‘. It shows how much ‚x‘ changes when ‚x‘ changes by 1.
  • The coefficient ‚b‘ is a constant term that represents the intersection of the linear function with the y-axis. It is the value of ‚x‘ if ‚x‘ is equal to zero.
  1. Constant (‚c‘): This is the known value to which the left side of the equation is supposed to be equal.

The goal of solving a linear equation is to find the value of the variable ‚x‘ that makes the equation true. In other words, you’re looking for the value of ‚x‘ that satisfies the equation ‚ax + b = c‘.

Here are some basic steps to solve a linear equation:

  1. Isolate the variable ‚x‘ Try to isolate the variable ‚x‘ on one side of the equation by using mathematical operations to simplify the equation. The goal is to have ‚x‘ alone on one side of the equation.
  2. Calculating the value of ‚x‘: After the equation in the form ‚x = …‘ calculate the value of ‚x‘.
  3. Checking the solution: Insert the calculated value of ‚x‘ into the original equation to make sure it’s true.


Suppose we have the equation ‚2x + 3 = 7‘. To isolate ‚x‘, we first subtract 3 from both sides of the equation:

2x + 3 – 3 = 7 – 3
2x = 4

Now we divide both sides of the equation by 2 to isolate ‚x‘:

(2x)/2 = 4/2
x = 2

The solution of the equation is ‚x = 2‘, and if we insert this solution into the original equation (‚2 * 2 + 3 = 7‘), we get ‚4 + 3 = 7‘, which is true. Therefore, ‚x = 2‘ is the correct solution.

It may sound a little complicated to convert this into a program, but it’s not. The simplest way is to simply solve the equation numerically, i.e. we simply insert values for x and do this until the equation is correct.
The whole thing is not very efficient and can be a bit tedious, especially with large values.
Another challenge is the so-called parsing of the formula. The computer doesn’t do anything with ‚4x+6=10‘ – it’s just a string for it. We need to break down the formula so that we can make it understandable for a program.
Thanks to the many libraries of Python and a practice program for our Linear Equations, we can keep it simple.
The SymPy library is ideally suited for our purposes.
SymPy is an open-source library and freely available.
SymPy is a powerful Python library for symbolic math. It offers a comprehensive collection of features and tools that make it possible to perform mathematical calculations in Python at a symbolic level. Unlike numerical mathematics, which calculates values, symbolic mathematics allows mathematical expressions, variables, and symbols to be manipulated to solve complex mathematical problems.
With SymPy, you can perform a variety of math tasks, including:
Algebraic manipulations: simplification of expressions, factorization, expansion of terms, and more.
Symbolic solving of equations and inequalities: SymPy can solve algebraic and transcendent equations and simplify inequalities.
Derivatives and Integration: Calculation of derivatives and integrations of symbolic expressions.
Linear algebra: working with matrices, determinants, eigenvalues, and eigenvectors.
Trigonometry and Complex Numbers: SymPy supports trigonometric functions, complex numbers, and their operations.
Differential equations: Solving ordinary and partial differential equations.
Geometry: SymPy can represent geometric shapes and concepts and perform calculations needed in geometry.
Number theory: Factorization of integers and working with prime numbers.
But enough with all the theory, let’s start banging on the keys and writing a trainer for the linear equations.
The first thing we need to do is install the SymPy in our environment. To do this, we open the command prompt and type
„python -m pip install sympy“
After a few seconds, it is installed.

So now we can start

import random
import sympy as sp

We import the two libraries we need. We give Sympy a so-called alias so that we don’t get calluses on our fingers.

Next, we need to declare three variables (a,b,c) and then populate them with random numbers.
We’ll put the whole thing into one function right away

def lineare_gleichung_loesen():
# Eingabe der Gleichung
a =  random.randint(1, 10)
b = random.randint(1, 10)
c = random.randint(1, 10)

print(f"Loese die linearen Gleichung '{a}x + {b} = {c}'")

For simplicity’s sake, we take numbers from 1 to 10 for a, b, and c.
For example, the program would now output 1x+5=5.
Now the user has to solve this equation and enter the answer.

user_input = input(f"x= ")

Now we have to teach the program how to solve the equation.
To do this, we need to use SymPy to create the equation.

# Create Equation
x = sp.symbols(‚x‘)
Equation = sp. Eq(a*x+b,c)

That was easy, wasn’t it?
Now let’s let SymPy solve the equation.

# Solve equation
Solution = sp.solve(equation, x)

So now our little trainer is ready
The whole program

import random
from fractions import Fraction
import sympy as sp

def lineare_gleichung_loesen():
    # Eingabe der Gleichung
    a =  random.randint(1, 10)
    b = random.randint(1, 10)
    c = random.randint(1, 10)

    print(f"Loese die linearen Gleichung '{a}x + {b} = {c}'")
    user_input = input(f"x= ")
    # Gleichung erstellen
    x = sp.symbols('x')
    gleichung = sp.Eq(a * x + b, c)

    # Gleichung lösen
    loesung = sp.solve(gleichung, x)
    # Ergebnis ausgeben
    print(f"Die Loesung der Gleichung {a}x + {b} = {c} ist x =", loesung)
    if float(Fraction(user_input)) == float(loesung[0]):
        print ("Leider falsch!");

if __name__ == "__main__":
    while True:
        weitermachen = input("Willst du eine weitere Gleichung loesen? (Ja/Nein): ").lower()
        if weitermachen != "ja":

The source code can be viewed or downloaded at gpiwonka/Linear Equations (