Math and Python: A Great Valentine’s Day Couple
Python + Math = true love!
This Valentine’s Day activity had a simple beginning. My good friend, Julie Riggins, and I were asked the question, “Will you do a Valentine's Day themed webinar?” Did we have a large bank of activities to draw from? Did we have a single idea? Nope. So, of course we said yes. We leaped straight off the cliff. Whether we figured out how to fly, or crashed horribly into the ground, is for someone else to decide.
We each came up with an idea. Riggins created a wonderful activity. While using Python, students learn about slope as they work to deliver flowers from Jack’s house to Diane’s house. (Yes, “Jack & Diane” dates us a little bit, but it’s a fun song.) By the way, I love Riggins’ virtual flowers.
If you want to learn more about Riggins’ activity, read to the end, and you can watch the webinar from Feb. 8, 2022, here.
For me, I decided to create an activity where students use Python to shoot cupid’s arrow through a heart while learning about linear and quadratic functions. This activity can be done either on a TINspire™ CX II or TI84 Plus CE Python graphing calculator. The commands are the same, but the menus are arranged differently. Note that in this blog, I have used the TINspire™ CX II graphing calculator to demonstrate the activity, and all screenshots have been taken from this handheld.
Is your heart beating faster? Maybe even skipping a beat? Let’s take a closer look at how to create a Python program that has cupid shooting his arrow through a heart, and it just might even win over your students’ hearts in math class.
Getting started and setting up the graph
First, we must lay the foundation for the activity. Here we are going to start the program and add in all the basic graphing elements. In Python, you need to define your window and put in your axes. To help see where the points are located, we will also add a grid to our graph. Let's add some color too, because everything is better in color!.
First, you will need to open a new document and add a Python Editor.
Doc > File > New Document > Add Python > New
Now, have your students type in a name for their program. Click on OK. I named my program “heart.”
We are going to use Python’s PlotLib module. Python is designed to use as few resources as possible. Each module can add functions, classes and variables. Basically, each one adds more commands to be used. Think of Python as a giant bookshelf that only has a few books on it. Adding a module is like adding a new book and thus new information to the bookshelf. The PlotLib will add commands to let us set up and draw our graph.
Add the PlotLib module: menu > TI PlotLib > import ti_plotlib as plt
Now that we have access to the commands we need, have the students set their window, add the grid, add the axes and pick a color.
Once the students have added these elements, I would encourage the students to play for a little bit. Have them run the program and see what happens. To run a Python program, press ctrl then R (ctrl + R). Ask them to rearrange the commands and run it again. What happens? What do they learn from the results? Have them play with the color. The color is based on a mixture of red, green and blue. Each of the colors can vary in value from 0 to 255. What colors can they make?
Preparing for the picture (and some math)
We are going to shoot an arrow through a heart. So, we need to draw a heart. And, if you are going to draw a picture, you need dots. How else are you supposed to draw a picture if you don’t have dots to connect? (My best artwork always involved books where you connect the dots.) We are going to graph our dots by graphing a scatterplot. First, we will type in two lists:
xh = [3,5,7,0,4,6,3,5]
yh = [0,0,0,6,2,2,3,4]
I chose xh to be the name for the xcoordinates of the heart and yh to be the ycoordinates of the heart. You can use any names you want. Have fun. Personally, I went for short names.
Using the lists, we can now draw the scatterplot. Have the students put in the code and run the program.
(ctrl + R)
Can they make the dots red?
Drawing the first linear function from (3,0) to (5,4)
As we start drawing the heart, we are going to move away from the most efficient program in favor of emphasizing the math. To do this, we are going to use Python’s function structure.
The first function is going to draw a line from the point (3,0) to the point (5,4). This will be the left side of the bottom of the heart. I would help the students put in the function and the plt.line command, but I would have them decide what function to put beside the “return” command. Have them run the program after they come up with their function. If the line does not connect the two points, have them try again. The graph of the line will serve as formative assessment for the students. Be sure to encourage your students if the line is incorrect. Coding is always a process of testing and correcting code.
Note about the command plt.line: The 3 and f1(3) are the x,ycoordinates of the first point of the line segment being plotted, and the 5 and f1(5) are the x,ycoordinate of the last point. This is a nice opportunity to make a connection to domain of a function or limiting a domain.
Draw the second linear function from (5,4) to (7,0)
The second linear function is going to draw a line from the point (5,4) to the point (7,0). This will be the right side of the bottom of the heart. Ask your students to add new code and use the same structure as the first line to graph this second line. Again, have the students run the program to see if they have the correct line.
Drawing the two quadratic functions through (3,0), (4,2), (5,0) and (5,0), (6,2), (7,0)
Again, we are going to use the function structure and the plot line command. There is not a predefined command to graph curved functions in Python, but they are still easy to graph. We are going to use a “for loop” to draw a lot of very short little lines to produce our quadratic functions.
Here is the code for the two quadratic functions:
Notes about the quadratic code:
 It may seem strange at first for the range to run from 30 to 50 in the “for loop,” but Python does not allow steps that are not integers. So, if you want to take a small step, you have to be creative. We want to run a graph from 3 to 5 with steps of a tenth. So, we go from 30 to 50 with steps of 1 and then divide each i by 10.
 Python represents an exponent using **. So, if you want x squared, you would write, x**2.
 I would recommend approaching the quadratic functions exactly like the linear functions. I would help the students put in and understand the code for the first quadratic function, but I would not give them the equation for the first quadratic function. I would have them write the code for the second quadratic function themselves.
 If you have not covered quadratic functions by Valentine’s Day, I would recommend connecting all of the dots using linear functions. It may not look quite as good, but it still would be educational and fun.
Firing the arrow through the heart
This part of the code is just for fun. You can certainly ask the students to write the function that graphs a line between the points (6,0) and (3,3). I would give them the rest of the code and ask them what they think it does. Once they input the code, encourage them to run the program and have fun with it.
If the arrow is moving too fast, you can slow it down. Go back to the top of the program and add the time module:
menu > More Modules > Time > from time import*
You slow the loop down by adding a sleep command. To make it work, add sleep(0.03) in the loop right after the last plt.line command. Make sure the sleep command is indented. You can find sleep here: menu > More Modules > Time.
If you wish to see Riggins’ and my webinar, you can find it here. You can also download a handout of this activity to try with your students.
Most of all, have fun, and have a Happy Valentine’s Day!
About the author: Adam Pennell received his undergraduate degree from the University of North Carolina and completed his doctorate in mathematics at North Carolina State University. He taught for three years at Bluefield College, and he has spent the last 24 years teaching at Greensboro College. He is a T³™ National Instructor, and he loves exploring new ways to help students learn. When Pennell is not teaching, he enjoys spending time with his wife and son. He has been married to Janet for 30 years, and his son is almost 12.
Tagcloud
Archive
 2023
 2022

2021
 January (2)
 February (3)
 March (5)

April (7)
 Top Tips for Tackling the SAT® with the TI84 Plus CE
 Which TI Calculator for the SAT® and Why?
 Monday Night Calculus With Steve Kokoska and Tom Dick
 Top Tips From a Math Teacher for Taking the Online AP® Exam
 How To Use the TI84 Plus Family of Graphing Calculators To Succeed on the ACT®
 Celebrate National Robotics Week With Supervised Teardowns
 AP® Statistics: 6 Math Functions You Must Know for the TI84 Plus
 May (1)
 June (3)
 July (2)
 August (5)
 September (2)

October (4)
 Transformation Graphing — the Families of Functions Modular Video Series to the Rescue!
 Top 3 HalloweenThemed Classroom Activities
 In Honor of National Chemistry Week, 5 “Organic” Ways to Incorporate TI Technology Into Chemistry Class
 5 Spooktacular Ways to Bring the Halloween “Spirits” Into Your Classroom
 November (4)
 December (1)

2020
 January (2)
 February (1)
 March (3)
 April (1)
 May (2)
 July (1)
 August (2)
 September (3)

October (7)
 Tips for Teachers in the time of COVID19
 Top 10 Features of TI84 Plus for Taking the ACT®
 TI Codes Contest Winners Revealed
 Best of Physics Activities for the Fall Semester
 Best of Middle Grades Science Activities
 Best of Biology Activities for the Fall Semester
 Best of Chemistry Activities for the Fall Semester
 November (1)
 December (2)
 2019

2018
 January (1)
 February (5)
 March (4)
 April (5)
 May (4)
 June (4)
 July (4)
 August (4)
 September (5)

October (9)
 Art in Chemistry
 Which Texas Instruments (TI) Calculator for the ACT® and Why?
 Meet TI Teacher of the Month: Jessica Kohout
 Innovation in Biology
 Learning With Your Students
 A firstofitskind STEM strategy charts path to help educators
 #NCTMregionals Hartford 2018 Recap
 The Math Behind “Going Viral”
 RealWorld Applications of Chemistry

November (8)
 Testing Tips: Using Calculators on Class Assessments
 Girls in STEM: A Personal Perspective
 5 Teachers You Should Be Following on Instagram Right Now
 Meet TI Teacher of the Month: Katie England
 EndofMarking Period Feedback Is a TwoWay Street
 #NCTMregionals Kansas City 2018 Recap
 Slope: It Shouldn’t Just Be a Formula
 Hit a high note exploring the math behind music
 December (5)
 2017
 2016
 2015