Learning Python 3 with the Linkbot/Lists

Variables with more than one value
You have already seen ordinary variables that store a single value. However other variable types can hold more than one value. These are called containers because they can contain more than one object. The simplest type is called a list. Here is an example of a list being used:

and an output example:

What month (1-12)? 3 The month is March

In this example the  is a list. is defined with the lines  and   (note that a   could also be used to split a long line, but that is not necessary in this case because Python is intelligent enough to recognize that everything within brackets belongs together). The  and   start and end the list with commas  separating the list items. The list is used in. A list consists of items that are numbered starting at 0. In other words, if you wanted January you would use. Give a list a number and it will return the value that is stored at that location.

The statement  will only be true if   is between one and twelve inclusive (in other words it is what you would expect if you have seen that in algebra).

Lists can be thought of as a series of boxes. Each box has a different value. For example, the boxes created by   would look like this:

Each box is referenced by its number so the statement  would get ,   would get   and so on up to   getting.

More features of lists
The next example is just to show a lot of other stuff lists can do (for once I don't expect you to type it in, but you should probably play around with lists in interactive mode until you are comfortable with them.). Here goes:

The output is:

demolist = ['life', 42, 'the universe', 6, 'and', 9] after 'everything' was appended demolist is now: ['life', 42, 'the universe', 6, 'and', 9, 'everything'] len(demolist) = 7 demolist.index(42) = 1 demolist[1] = 42 demolist[ 0 ] = life demolist[ 1 ] = 42 demolist[ 2 ] = the universe demolist[ 3 ] = 6 demolist[ 4 ] = and demolist[ 5 ] = 9 demolist[ 6 ] = everything After 'the universe' was removed demolist is now: ['life', 42, 6, 'and', 9, 'everything'] 'life' was found in demolist 'amoeba' was not found in demolist The sorted another_list is [0, 7, 42, 123]

This example uses a whole bunch of new functions. Notice that you can just  a whole list. Next the  function is used to add a new item to the end of the list. returns how many items are in a list. The valid indexes (as in numbers that can be used inside of the ) of a list range from 0 to. The function tells where the first location of an item is located in a list. Notice how  returns 1, and when  is run it returns 42. To get help on all the functions a list provides for you, type  in the interactive Python interpreter.

The line  is a just a reminder to the programmer (also called a comment). Python ignores everything that is written after a  on the current line. Next the lines:

create a variable, which starts at 0 and is incremented until it reaches the last index of the list. Meanwhile, the  statement prints out each element of the list.

A much better way to do the above is:

The  command can be used to remove a given element in a list. The next few lines use the  operator to test if an element is in or is not in a list. The  function sorts the list. This is useful if you need a list in order from smallest number to largest or alphabetical. Note that this rearranges the list. In summary, for a list, the following operations occur:

This next example uses these features in a more useful way:

And here is part of the output:

1. Print the list 2. Add a name to the list 3. Remove a name from the list 4. Change an item in the list 9. Quit Pick an item from the menu: 2 Type in a name to add: Jack Pick an item from the menu: 2 Type in a name to add: Jill Pick an item from the menu: 1 0 . Jack 1 . Jill Pick an item from the menu: 3 What name would you like to remove: Jack Pick an item from the menu: 4 What name would you like to change: Jill What is the new name: Jill Peters Pick an item from the menu: 1 0 . Jill Peters Pick an item from the menu: 9 Goodbye

That was a long program. Let's take a look at the source code. The line  makes the variable   a list with no items (or elements). The next important line is. This line starts a loop that allows the menu system for this program. The next few lines display a menu and decide which part of the program to run.

The section goes through the list and prints each name. tells how many items are in the list. If  returns , then the list is empty.

Then, a few lines later, the statement  appears. It uses the  function to add an item to the end of the list. Jump down another two lines, and notice this section of code: Here the  function is used to find the index value that will be used later to remove the item. is used to remove an element of the list.

The next section uses  to find the   and then puts   where the   was.

Congratulations, with lists under your belt, you now know enough of the language that you could do any computations that a computer can do (this is technically known as Turing-Completeness). Of course, there are still many features that are used to make your life easier.

Examples
test.py

The values  and   point to 1 and 0, respectively. They are often used in sanity checks, loop conditions etc. You will learn more about this a little bit later (chapter Boolean Expressions). Please note that get_questions is essentially a list because even though it's technically a function, returning a list of lists is the only thing it does.

Sample Output:

What color is the daytime sky on a clear day? green Incorrect, correct was: blue What is the answer to life, the universe and everything? 42 Correct What is a three letter word for mouse trap? cat Correct You got 66 % right out of 3

LinkbotMelody.py

We can also create our own lists of items and use for loops to loop through them. Lets try making a list of keyboard keys to play in order to play a simple tune. We'll write this program using the knowledge that middle-C is the 40th key on our keyboard. When you run this program, it should play the beginning of a very familiar tune. Can you guess what it is?

Exercises
Expand the test.py program so it has a menu giving the option of taking the test, viewing the list of questions and answers, and an option to quit. Also, add a new question to ask, "What noise does a truly advanced machine make?" with the answer of "ping".