Learn to Program

Arrays and Iterators

Chapter 7

 

Let's write a program which asks us to type in as many words as we want (one word per line, continuing until we just press Enter on an empty line), and which then repeats the words back to us in alphabetical order. OK?

So... first we'll—uh... um... hmmm... Well, we could—er... um...

You know, I don't think we can do it. We need a way to store an unknown amount of words, and how to keep track of them all together, so they don't get mixed up with other variables. We need to put them in some sort of a list. We need arrays.

An array is just a list in your computer. Every slot in the list acts like a variable: you can see what object a particular slot points to, and you can make it point to a different object. Let's take a look at some arrays:

[]
[5]
['Hello', 'Goodbye']

flavor = 'vanilla'             # This is not an array, of course...
[89.9, flavor, [True, False]]  # ...but this is.

So first we have an empty array, then an array holding a single number, then an array holding two strings. Next, we have a simple assignment; then an array holding three objects, the last of which is the array [TrueFalse]. Remember, variables aren't objects, so our last array is really pointing to float, a string, and an array. Even if we were to set flavor to point to something else, that wouldn't change the array.

To help us find a particular object in an array, each slot is given an index number. Programmers (and, incidentally, most mathematicians) start counting from zero, though, so the first slot in the array is slot zero. Here's how we would reference the objects in an array:

names = ['Ada', 'Belle', 'Chris']

print(names)
print(names[0])
print(names[1])
print(names[2])
print(names[3])  # This is out of range.
['Ada', 'Belle', 'Chris']
Ada
Belle
Chris
IndexError: list index out of range

So, we see that print(names) prints names just like you write it in a program file. Then we use print(names[0]) to print out the "first" name in the array, and print(names[1]) to print the "second"... I'm sure this seems confusing, but you do get used to it. You just have to really start thinking that counting begins at zero, and stop using words like "first" and "second". If you go out to a five-course meal, don't talk about the "first" course; talk about course zero (and in your head, be thinking course[0]). You have five fingers on your right hand, and their numbers are 0, 1, 2, 3, and 4. My wife and I are jugglers. When we juggle six clubs, we are juggling clubs 0-5. Hopefully in the next few months, we'll be able to juggle club 6 (and thus be juggling seven clubs between us). You'll know you've got it when you start using the word "zeroth". :-) Yes, it's a real word; ask any programmer or mathematician.

Finally, we tried print(names[3]), just to see what would happen. Were you expecting an error? You were right! Python is telling us that we can't ask for an index that isn't in the array.

If all this funny numbering of array slots is getting to you, fear not! Often, we can avoid them completely by using different language features, like this one:

for...in

for allows us to do something (whatever we want) for each object the array points to. So, if we want to say something nice about each language in the array below, we'd do this:

languages = ['English', 'German', 'Python']

for lang in languages:
  print('I love ' + lang + '!')
  print('Don\'t you?')

print('And let\'s hear it for C++!')
print('...')
I love English!
Don't you?
I love German!
Don't you?
I love Python!
Don't you?
And let's hear it for C++!
...

So what just happened? Well, we were able to go through every object in the array without using any numbers, so that's definitely nice. Translating into English, the above program reads something like: for each object in languages, point the variable lang to the object and then do everything I tell you to that is tabbed to the right. (Just so you know, C++ is another programming language. It's much harder to learn than Python; usually, a C++ program will be many times longer than a Python program which does the same thing.)

Here's a cute way to do the same thing again and again!

for _ in range(3):
  print('Hip-Hip-Hooray!')
Hip-Hip-Hooray!
Hip-Hip-Hooray!
Hip-Hip-Hooray!

Here, _ is a variable. We commonly use the _ as a placeholder variable name when we don't intend to use the variable; we only need it for the for.

More Array Methods

TODO: removed paragraph: problems: refers to "for" as a "method", and requires chapter 5 for references to previous string methods

First, let's look at str and join. join works much like str does, except that it adds a string in between the array's objects. Let's take a look:

foods = ['artichoke', 'brioche', 'caramel']

print(str(foods))
print()
print(', '.join(foods))
print()
print('  :)  '.join(foods) + '  8)')
["artichoke", "brioche", "caramel"]

artichoke, brioche, caramel

artichoke  :)  brioche  :)  caramel  8)

Did you notice that I left out the empty strings when I wanted to print a blank line? It does the same thing.

Now let's take a look at push and pop. The methods push and pop are sort of opposites, like + and - are. push adds an object to the end of your array, and pop removes the last object from the array (and tell you what it was). Again, push and pop actually change the array:

favorites = []
favorites.push('raindrops on roses')
favorites.push('whiskey on kittens')

print(favorites[0])
print(favorites[1])
print(len(favorites))

print(favorites.pop())
print(favorites)
print(len(favorites))
raindrops on roses
whiskey on kittens
2
whiskey on kittens
raindrops on roses
1

A Few Things to Try

So far we have learned quite a number of different methods. Now it's time to learn how to make our own.