Using Python Coding to Boost Students’ Interest in Algebra
Using basic Python to check work in math class is fun and introduces students to valuable coding basics.
Your content has been saved!
Go to My Saved Content.Spicing up Algebra I class isn’t easy, and getting students to check their answers can be especially challenging. However, introducing short Python programs to check answers is easy and fun, and your students will even learn some coding basics. Adding some Python can help students’ overall academic performance and their learning of problem-solving skills. It can also increase adherence to Math Common Core standards about technology integration.
You may even get your students excited about algebra class because most students think Python programming is fun. The best things for me were that I could tell that students were enjoying algebra class. I didn’t even get the usual blank stares when I explained what a variable is in algebra because they had already seen variables in Python. No programming experience is necessary for teachers or students, and implementation takes little time. All you need to do is learn a few Python basics, some specifics for our answer-checking program, and very basic troubleshooting.
getting started with python
My Algebra I class is all special education students, and several of them told me that math was their favorite subject because of Python. My students said that when they thought of math class, they thought of boring worksheets, but doing some of the math on the computer made it seem fun and not like schoolwork. I also teach general education STEM, including AP, and I have used Python in these classes with similar results. Adding some Python programming can spice up many courses for many types of students.
To get started with Python, you need computers with internet access. Teachers can install Python on class computers. However, I recommend using JDoodle, which is free on the web. It is simpler and will work well for our purposes. Making an account for JDoodle will be a time-saver for both students and teachers. Once you are set up with JDoodle, you can dive right into Python.
basic coding structure
Our goal is to learn enough Python to understand the code structure, modify the code to check different algebra problems, and troubleshoot. After you get Python set up for your students, the next step is to teach them a bit about the language, including variables, equations, and output. We cover only what Python is necessary to understand our algebra-checking program. My students got to the point where they could modify the program themselves to check their answers.
Variables: In Python, variables do not have to be declared, which makes it different from many other computer languages. You can just use an equal sign to assign values; for example, sum = 2 + 2. The variable (sum) being assigned a value is always at the left of the equal sign.
Equations: The next important Python concept is math equations. In Python, math equations are straightforward: + is used for addition, - for subtraction, * for multiplication, and / for division. There are other types of operations, but we will only review what is needed for the algebra we are doing to check answers. I recommend parentheses for easy code checking and fewer coding errors.
Writing the code
Now that we have some basic understanding of Python, the next step is to apply that to our answer-checking program. Here is the code to check an answer to an example problem: 6x + 2 = 20.
Line 1: x=3
Line 2: if (6*x + 2 == 20):
Line 3: print ("You did great.")
Line 4: else:
Line 5: print ("Try again!")
We will learn step-by-step what each line of code does.
Line 1 assigns a value to a variable. In this case, the value of x in our program is now 3 (which is the students’ answer from doing their own work).
Lines 3 and 5 print output to the computer screen. I let my students modify what was in the print statements in order to personalize their programs—as long as it is appropriate. All teachers can see this coming: A student coded a very funny print statement... which was not rated G. I could not help laughing, but they lost their computer privileges for that period anyway. But that is how I knew Python was a hit—there were no other issues. No one wanted to lose their computer for the class period.
Lines 2 and 4 are a conditional (if else) statement. A conditional statement will execute a line of code (in our case, line 3) if the condition is true and another line if the condition is false (line 5 in this code).
Our original equation, 6x + 2 = 20, looks like this (6*x + 2 == 20) when converted into Python. The outside parentheses are needed because the statement inside them is what the Python interpreter will evaluate. The double equal sign is the comparison operator. Python checks whether 6 x (3) + 2 = 20.
Running and Troubleshooting the Code
There is a window to type in your code. To run your code, make sure the “Interactive” box is checked. Click the “Execute” button. Your output will appear on the screen.
If you run the program and get an error message, you need to troubleshoot. The coding and running of a program are fairly straightforward, but sometimes the program fails to run properly. The troubleshooting can be the most time-consuming and challenging part of coding until you get used to it. The most important thing to remember in Python is that indentation is particularly important. Even if the indentation looks the same, but one indentation is spaces and the other tabs, the program will not run correctly. All details, including spelling, capitalization, and indentation, are important, and the program will not run correctly if there are even minor mistakes.
One way to troubleshoot this code would be to make sure it looks exactly like the code in the example and then just change the numbers. I tell my students to use the space key and not the tab, and that wards off problems. When doing troubleshooting for students, you can keep a copy of the working code on your JDoodle account, cut and paste, and then just change the numbers to fit the problem you are checking.
Python answer checking is fun and easy for my students and is also beneficial. The use of variables in this program demonstrates concretely what variables do, making the concept much less abstract. We used Python whenever we solved algebra equations in one variable, and that is a large part of Algebra 1 class. As soon as the students came into the classroom, they got out their computers and brought up JDoodle and used it whenever there was an answer to check.