[Edu-sig] what you can do in a shell

Michel Paul mpaul at bhusd.k12.ca.us
Thu Sep 28 02:20:36 CEST 2006


Here's a case where I was able to weave Python into math class a little unexpectedly -

The other day students were confused by why we subtract h in y = f(x - h) when we translate f horizontally h units.  So I fired up Python and did a shell session with them.  Here is something similar to what we did:

>>> ## Let's define a function.  Any function.  It doesn't have to be too complicated.
>>> def f(x): return 2**x - 1

>>> ## Now let's generate a table of ordered pairs:
>>> for x in range(-5, 6): (x, f(x))

(-5, -0.96875)
(-4, -0.9375)
(-3, -0.875)
(-2, -0.75)
(-1, -0.5)
(0, 0)
(1, 1)
(2, 3)
(3, 7)
(4, 15)
(5, 31)
>>> ## Now let's horizontally translate this function.
>>> ## By how much shall we translate it?
>>> h = 3
>>> ## OK, let's see ... we want to shift our domain ...
>>> for x in range(-5 + h, 6 + h): (x, f(x))

(-2, -0.75)
(-1, -0.5)
(0, 0)
(1, 1)
(2, 3)
(3, 7)
(4, 15)
(5, 31)
(6, 63)
(7, 127)
(8, 255)
>>> ## Hmmm ... what happened?
>>> ## Our x values translated OK, but our y values are off.
>>> ## In order to be a horizontal translation, our y values should remain the same.
>>> ## Oh no! What can we do?
>>> ## <discussion>
>>> for x in range(-5 + h, 6 + h): (x, f(x - h))

(-2, -0.96875)
(-1, -0.9375)
(0, -0.875)
(1, -0.75)
(2, -0.5)
(3, 0)
(4, 1)
(5, 3)
(6, 7)
(7, 15)
(8, 31)
>>> ## Ahh!  That's better!
>>> ## So, when we translate x by h, we preserve the y-value back at x - h:
>>> ##        y = f(x - h)

This is the beauty of the Python shell - a math student doesn't have to know any Python syntax to be able to follow this.  They can just see it as active Algebra.  Sure, you have to make clear how the upper limit in range works, and you have to make sure they don't get confused when we're using "range" in specifying a "domain", but still, they were able to follow what was going on, and thinking the issue through in this way was helpful.  Not fancy stuff, not OO, but useful.



More information about the Edu-sig mailing list