2016 September 16,

CS 111: Lists

As our programs get bigger and bigger, with more and more variables, we will need to organize those variables in some way. A data structure is a "super-variable" that binds many variables together in a systematic way. There are many kinds of data structures, and CS 201 is all about them. Here we learn about one of the most basic kinds: lists.

A list is a sequence of objects of any type: integers, floating-point numbers, Booleans, strings, or anything else in Python. You enter a list using square brackets [] and commas.

friendList = ["Jane", "Moon Unit", 2.718, "Babatope"]

friendList

type(friendList)

Many of the basic operations for strings also work on lists, in a similar way.

friendList[0]

friendList[1]

friendList[1:2]

len(friendList)

enemyList = ["Daniel Schorr", "Barney Gumble"]

friendList + enemyList

You can alter the contents of a list, by setting any list element to a new value:

friendList[2] = "Ken doll"

friendList

Question 13A: What happens if you have a string and you try to alter its index-2 character in this way?

Question 13B: Write a program that starts with a list of numbers, uses a loop (either while or for) to replace each number with its square, and then prints the list. For example, if the first line of the program is l = [2, -2.3, 17, 1], then the program changes l to [4, 5.289999999999999, 289, 1] and prints it out.

The elements of a list can be Python objects of any type, including lists. Check this out carefully:

myList = [[1, 9, -2], [2, 4, 11], [12, -98, 1]]

myList[1]

type(myList[1])

myList[0]

myList[0][1]

type(myList[0][1])

myList[2] = 13

myList

myList[0][2] = myList[0][2] + myList[2]

myList

Now we come to extreme danger (or what counts for extreme danger, to a computer programmer). Consider:

list2 = [2, 7, 1, 3]

list3 = list2

list3[2] = 17

list3

Question 13C: What is list2, at the end of those commands?

Remember that a variable is essentially a spot in the computer's memory, where you can store a value. You can name your variables however you like, and store whatever you like in them. In this case, we stored a list in the variable list2. Then we made a new variable, list3, and set it to have the same value as list2. When we changed the contents of the list list3, the value of the list list2 also changed, because list3 and list2 refer to the same spot in the computer's memory. When you alter one of them, you automatically alter the other. This problem is called aliasing. (Just as a criminal can have multiple names, called aliases, so can a spot in a computer's memory.) Aliasing can cause serious errors that are difficult to find. We'll discuss how to avoid these errors later.

This next question is about the game Tic Tac Toe. This game is played on a 3x3 board. Each space on the board is either empty, or contains an X, or contains an O. If any row, column, or diagonal contains three Xs, then the X player wins; if any row, column, or diagonal contains three Os, then the O player wins. It never happens, that both players have won at the same time. (There are other rules of course, but they aren't relevant to my question.) We can represent a Tic Tac Toe board in Python as a list containing three lists, where each of these is a list of three strings, and each string is either "X", "O", or " ". For example, here is a board in which the O player has won:

b = [["O", "X", "X"], [" ", "O", " "], [" ", "X", "O"]]

It helps if you write the three sublists on top of each other, like a 3x3 grid. Then you will see that O has a diagonal. The Xs are at b[0][1], b[0][2], and b[2][1], right?

Question 13D: Write a program that starts with a Tic Tac Toe board, as just described, and prints out a message saying that X has won, that O has won, or that neither has won. Of course, your program should work on any valid Tic Tac Toe board, not just the one shown. I'm not psychic, but I see if statements in your future.