what you can do in a shell
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.
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.
Yes, exactly: Pythonic Algebra is naturally self-teaching, plus you can do it without some adult standing over you, watching you make mistakes. The Python interpreter already knows you'll make mistakes, has a lot of stuff builtin to help you with that. Branching off on your idea of generating ordered pairs, I've seen various trix like these: IDLE 1.2b2
def f(x): return x # simplest linear
[(x,f(x)) for x in range(5)] [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
def g(x,m=1,b=0): return m*x + b # slope-intercept linear
[(x , g( x, 0.25, 2)) for x in range(5)]
[(0, 2.0), (1, 2.25), (2, 2.5), (3, 2.75), (4, 3.0)] Then if you want a VPython plot, you can do something like:
import stickworks # code given earlier this month
def domain(): x = -5 while True: yield x x += .5
dom = domain() plot = stickworks.xyplotter(dom, g) plot.next() # VPython window appears with line segment plot.next() # line segment gets longer plot.next() # and longer... plot.next() # and longer... ...
Except that's not the smartest way to go (e.g. no axes shown yet). Better to just modify stickworks.testme. def testme(): """ >>> from stickworks import testme Visual 2005-01-08 >>> testme() See: http://www.4dsolutions.net/ocn/graphics/cosines.png """ from math import cos def f(x): return cos(x) def dgen(start, step): while True: yield start start += step d = dgen(-5, 0.1) axes(-5,1,0) graph = xyplotter(d, f) for i in xrange(100): graph.next() Kirby
Question: The PEP on the 2.5 release schedule (2.5 Final already reached), claims PEP 309 was somewhere implemented -- some way to curry functions with partial. Anyone know where it is. Our own edu-sig's Scott... http://www.python.org/dev/peps/pep-0356/ Oh wait, I just found it:
import functools dir(functools) ['WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '__builtins__', '__doc__', '__file__', '__name__', 'partial', 'update_wrapper', 'wraps']
So using that same g from before:
def g(x,m=1,b=0): return m*x + b # slope-intercept linear
G = functools.partial(g,m=0.25,b=2) G <functools.partial object at 0x00D0ACF0> [(x,G(x)) for x in range(8)] [(0, 2.0), (1, 2.25), (2, 2.5), (3, 2.75), (4, 3.0), (5, 3.25), (6, 3.5), (7, 3.75)]
Kwel. Kirby
On 27-Sep-06, at 6:24 PM, kirby urner wrote:
Question:
The PEP on the 2.5 release schedule (2.5 Final already reached), claims PEP 309 was somewhere implemented -- some way to curry functions with partial. Anyone know where it is. Our own edu-sig's Scott...
One of the best ways to get up to speed quickly with the new features in a new version of Python is with Andrew Kuchling's "What's New" article in the standard documentation. Here's the one for Python 2.5: http://docs.python.org/whatsnew/whatsnew25.html --Dethe
Oh wait, I just found it:
import functools dir(functools) ['WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '__builtins__', '__doc__', '__file__', '__name__', 'partial', 'update_wrapper', 'wraps']
So using that same g from before:
def g(x,m=1,b=0): return m*x + b # slope-intercept linear
G = functools.partial(g,m=0.25,b=2) G <functools.partial object at 0x00D0ACF0> [(x,G(x)) for x in range(8)] [(0, 2.0), (1, 2.25), (2, 2.5), (3, 2.75), (4, 3.0), (5, 3.25), (6, 3.5), (7, 3.75)]
Kwel.
Kirby _______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig
"Say what you like about C++, but it's uninitialized variables will always hold a special place in my heart. In a world where we define *everything* concretely it is the last refuge of the undefined. It's the programmer's Wild West, the untamed frontier." --Bjorn Stroustrap
On 9/28/06, Dethe Elza <delza@livingcode.org> wrote:
One of the best ways to get up to speed quickly with the new features in a new version of Python is with Andrew Kuchling's "What's New" article in the standard documentation.
Yes. Python.org is such a well organized site, I encourage people to visit it often. Kirby
participants (3)
-
Dethe Elza -
kirby urner -
Michel Paul