Algebra and Programming: How Mathematics Helps in Coding
· Updated
If you can solve equations, you are already halfway to programming: the variables, formulas and logic of algebra are in every program. Here is how, with simple examples in Python, one of the most popular first programming languages, easy to read and close to plain English.
Variables
In algebra, x is an unknown number. In a program, a variable is a name attached to a value. Writing a = 5 means “let a be 5.” The big difference is that in a program a variable’s value can change as the program runs. After the lines a = 5 and a = a + 1, a equals 6. In algebra that equation would be false; in a program it just means “add one to a.”
Formulas
Formulas in a program look almost the same as in your notebook. For a rectangle 4 long and 3 wide, the line s = 4 * 3 gives an area of 12, and b = 2 * a + 1 with a = 5 gives 11. The asterisk is the multiplication sign. To see the result, you write print(b).
Boolean Logic
The Boolean type takes its name from George Boole, who devised an algebra of logic in the mid-nineteenth century. A Boolean value has just two options, true or false, written True and False in Python. Comparisons return exactly these: 7 > 3 gives True, and 2 > 5 gives False. Conditions can be combined: x > 3 and x < 10 is true only if both hold, while with or one is enough. That is how a program decides what to do next, taking one action if a condition is true and another if it is false.
Try It Yourself
- Work out the perimeter of a square with side a = 6 using p = 4 * a.
- Check whether x = 12 lies between 10 and 20: x > 10 and x < 20.
- Think about what a equals after the lines a = 3 and a = a * 2.
Why It Matters
The more confidently a student handles formulas and logical conditions, the easier both algebra and programming become. How algebra comes in handy in exams is covered in The Role of Online Math Tutors in Exam Preparation.
How to Get Started
In our algebra course, the teacher helps students understand school topics and fill gaps, teaches them to spot patterns, compare solution methods and explain their reasoning, and prepares them for tests and exams. Lessons are open to students from age 13. A trial lesson costs $8.00: it is individual and lasts 45 minutes. More about the course is on the Algebra page.