Python Lesson 3 – Lists
May 9, 2014
[This is part of a series of posts on learning and teaching Python with my 10-year old son. Read the back story.]
Go back to Python Lesson 2 – Calulations and Variables
Chapter 3 in the Python for Kids book we’ve been using shows how data lists can be created, stored and modified using Python. These are powerful tools but it’s hard for my son to get too excited about them because he knows that Chapter 4 involves graphics programming and he can’t wait.
He created a few lists, mostly using strings, and figured out how to delete list items, append list items and combine with other lists. But, he wanted to move on and I wasn’t inclined to force him to see the value when he’s excited about something different. Here are a few examples from the book involving lists.
Here a list is represented as a string variable:
>>> wizard_list = ‘spider legs, toe of frog, eye of newt, bat wing, slug butter, snake dandruff’
>>> print(wizard_list)
spider legs, toe of frog, eye of newt, bat wing, slug butter, snake dandruff
But, a more powerful list can be created this way:
>>> wizard_list = [‘spider legs’, ‘toe of frog’, ‘eye of newt’, ‘bat wing’, ‘slug butter’, ‘snake dandruff’]
>>> print(wizard_list)
[‘spider legs’, ‘toe of frog’, ‘eye of newt’, ‘bat wing’, ‘slug butter’, ‘snake dandruff’]
By formatting a list rather than a string variable you can then refer to individual items on the list:
>>> print(wizard_list[2])
eye of newt
And here we learn that when computer programs and computer programmers count, they begin with zero so wizard_list[2] refers to the third item in the list. Hey, this is important stuff if you want to learn computer science.
Also, here’s how to change a list item:
>>> wizard_list[2] = ‘snail tongue’
>>> print(wizard_list)
[‘spider legs’, ‘toe of frog’, ‘snail tongue’, ‘bat wing’, ‘slug butter’, ‘snake dandruff’]
Here’s how you display a subset of the list:
>>> print(wizard_list[2:5])
[‘snail tongue’, ‘bat wing’, ‘slug butter’]
Here’s how you add items to a list:
>>> wizard_list.append(‘bear burp’)
>>> print(wizard_list)
[‘spider legs’, ‘toe of frog’, ‘snail tongue’, ‘bat wing’, ‘slug butter’, ‘snake dandruff’, ‘bear burp’]
Here’s how you remove items from a list:
>>> del wizard_list[5]
>>> print(wizard_list)
[‘spider legs’, ‘toe of frog’, ‘snail tongue’, ‘bat wing’, ‘slug butter’, ‘bear burp’, ‘mandrake’, ‘hemlock’, ‘swamp gas’]
There are, of course, many different things that can be done with lists along the same lines but, as I mentioned, my son was ready for graphics.
So we moved on to Chapter 4 with excitement about learning some elementary graphics programming. Unfortunately, in doing so, we discovered a problem with the pythonanywhere.com cloud setup. Apparently the graphics library won’t work so easily in this environment as it needs a new window to draw graphics and the cloud version doesn’t yet support the necessary functionality.
Short story, we will need a local Python install to work on the graphics stuff and that has slowed us down a bit. Back next week with some more interesting progress to report. In any case, we learned that pythonanywhere.com works anywhere but not for everything.