[Edu-sig] Cards n stuff...
Kirby Urner
pdx4d@teleport.com
Wed, 14 Mar 2001 11:34:56 -0800
At 01:34 PM 03/14/2001 -0500, you wrote:
>Thanks Kirby!
>
>This was really helpful. The one line I would change is:
>
> ranks = ['Ace']+map(str,range(2,11))+['Jack','Queen','King']
You could go:
ranks = ['Ace'] + [str(x) for x in range(2,11)] + ['Jack','Queen','King']
>I haven't discussed map at all yet, and actually don't plan to.
>
I think because of list comprehension syntax (as above), 'map'
and 'little lambda' are even less necessary than before.
I really like the power of list comprehension, and think
it makes sense to teach it e.g.:
>>> def f(x): return x*x
>>> [f(x) for x in range(5)] # successive 2nd powers
[0, 1, 4, 9, 16]
and
>>> [f(x) for x in range(5) if x%2==0] # even 2nd powers only
[0, 4, 16]
In a 'math through programming' context, it'll be combined
with 'zip' to create coordinate pairs, e.g.:
>>> zip(range(5),[f(x) for x in range(5)])
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
This starts to become graphable, with appropriate methods in
the background.
>In using Python to teach, simplicity takes on even more importance
>than it usually has (and it is always important ;-)
>
Yes, I understand. Usually you'll have a few students who
want to tackle some of the more 'advanced' syntax, so having
exercises set aside for them makes some sense too.
>Python is such a wonderful language for teaching precisely
>because it "fits your brain". In the case of students new
>to computing, "fitting their brains" means minimizing the number of
>previous concepts needed to understand a new concept.
>
Yes.
Also, though, it's great to start with kids without a lot of
preconceptions. They can start fresh, taking OO concepts for
granted (no paradigm shift required).
For example, the idea that 'map' distributes a function across
a sequence of inputs sort of fits our mental picture of a conveyor
belt, with someone squeezing frosting on each cupcake as it goes
by, or whatever it is:
>>> def add_frosting(thing): return thing + " with frosting"
>>> map(add_frosting, ['cupcake']*5)
['cupcake with frosting', 'cupcake with frosting', 'cupcake with frosting',
'cupcake with frosting', 'cupcake with frosting']
Once you've got some hooks to visuals or well-known models, then
these 'fancy' Pythonic expressions take on a more mundane appearance.
map(str,range(10)) simply 'waves the magic wand' over a list
0...9, where the 'wand', in this case, isn't add_frosting, but
the 'convert to string' function str(). I know you know this --
just trying to imagine how I'd explain it to someone with no clue.
>In truth, what is good for new learners is good in general,
>only more so.
>
>Thanks again for your help!
>
>jeff
I've sent you a number more since then. You probably won't
want to use stuff like:
ranks = eval( '{' +
','.join(["'%s':%s" % x for x in
zip(['Ace'] +
[str(x) for x in range(2,11)] +
['Jack','Queen','King'],
range(1,15))])
+ '}' )
But if your class is up for such a dictionary (using 'spelled out'
rank to look up corresponding integer value), a more loopy
initialization would be:
def mkrankdict():
j = 1
dict = {}
for i in ['Ace','2','3','4','5','6','7','8','9','10',
'Jack','Queen','King']:
dict[i]=j
j = j+1
return dict
ranks = mkrankdict()
>>> ranks = mkranddict()
>>> ranks
{'8': 8, '9': 9, '6': 6, '7': 7, 'Jack': 11, '5': 5, '2': 2,
'3': 3, 'Queen': 12, 'King': 13, 'Ace': 1, '4': 4, '10': 10}
That'll be easier to read for a lot of us. It'd be fun to show
these side-by-side, as two ways of getting the same result.
Kirby