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

kirby urner kirby.urner at gmail.com
Thu Sep 28 02:54:21 CEST 2006


> 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


More information about the Edu-sig mailing list