15-112 Fundamentals of Programming
Lecture 8September 14 , 2017
Announcements
Read pages 74 – 77
What are we doing today?
ListsList functions
Lists – Lets start with the “why”
Given a sequence of numbers, how do you find the minimum value?How do you find: max, sum, average?How do you find: How many numbers are
above average?
Lists as sequence of values
Variables can hold one value at a timeLists can hold a number of different values
Create a list
A simple listmyList = [1, 4, 5, 8, 12]
An initialized listmyList = [0] * 5myList[0,0,0,0,0]
Adding element to a lista = [0] * 5b= 12a = a + [b]a [0,0,0,0,0,12]
Creating Lists
Combining two listsmyList = [1, 4, 5, 8, 12]yourList = [3,7,4,9]ourList = myList + yourList ourList would be [1, 4, 5, 8, 12, 3, 7, 4, 9]
Accessing Elements
a = [2, 3, 5, 7, 11, 13]print a[0]print a[2]print a[-1]print a[-3]print a[0:2]print a[1:4]print a[1:6:2]
2
13
5
7[2,3]
[3, 7, 13]
[3, 5, 7]
Modifying Elements
a = [2, 3, 5, 7, 11, 13]
print aa[2] = 0print a
[2, 3, 5, 7, 11, 13]
[2, 3, 0, 7, 11, 13]
More List Operations
Slicing a ListmyList [1, 4, 5, 200, 12]print myList[1:3] [4, 5]
Assigning Value to a slicemyList[1:3] = [6,6,6,6]myList[1, 6, 6, 6, 6, 200, 12]
Finding Elements
Check for list membership: using “in”
a = [ 2, 3, 5, 2, 6, 2, 2, 7 ]print "a =", aprint (2 in a)print (4 in a)
Looping over Lists
a = [ 2, 3, 5, 7 ]
for item in a:print item
Exercise
Write a function called isSubset, that takes two lists as input parameters. The function should return True if all elements of first list are in second list.
Do you know the following about lists?
Can you declare an empty list?Can you declare a list with 5 1’s?Can you print the last element in a list?Can you access each element in a list using
a loop?Can you check if an element exists in a list?
So what’s the outputa = [2,3,6,7,1,4,8,8,8]for i in range(0,3):
print a[i*3+0],a[i*3+1],a[i*3+2]
a = [3,2,1,6,4,8,7,7,5]for i in range(0,3):
print a[i],a[i+3],a[i+6]
Output?def fun2(x,y):
for i in range(1,len(y)):if x[i] == y[-i]:
print i
fun2([7,3,5,2,4,8],[7,3,4,2,7,3])
Representing Data using lists
We can use lists to represent 1-D data. Grades of all students in Quiz1
How about 2-D data using a list? Grades of all students in all quizzes.
Tic-Tac-Toe
Suppose we have a list that represents a tic-tac-toe board. How big should be the list? How should we initialize it?
1 2 3
4 5 6
987
[1,2,3,4,5,6,7,8,9]
Printing the board
How do we print this list so it looks like a board?
printBoard(mylist)?
1 2 3
4 5 6
987
[1,2,3,4,5,6,7,8,9]
Accessing an element
How do I get an element at row 0, column 2
getElement(board,row,column)?
1 2 3
4 5 6
987
[1,2,3,4,5,6,7,8,9]
Setting an element
How do I set an element at row 0, column 2
setElement(board,row,column,player)?
1 2 3
4 5 6
987
[1,2,3,4,5,6,7,8,9]
Winning
How do we know if a player won?
gameWon(board,player)?
1 2 3
4 5 6
987
[1,2,3,4,5,6,7,8,9]
2-D lists
We have seen lists that hold integers, booleans, strings, etc.Can lists also hold other lists? [[1,2,3],[4,5,6],[7,8,9]]
+ How many elements in the above list? [1,2,[7,’a’,True],5,8]
+ How many elements in this list?
Accessing Elements in 2-D listsa = [[1,2,3],[4,5,6],[7,8,9]]
Reading valuesprint a[0][1]
Writing valuesa[1][0] = 12print a
[[1,2,3],[12,5,6],[7,8,9]]
Tic-Tac-Toe with 2-D lists
Suppose we use a 2D-list to represent a tic-tac-toe board. How big should be the list? How should we initialize it?
1 2 3
4 5 6
987
[[1,2,3],[4,5,6],[7,8,9]]
Printing the board
How do we print this list so it looks like a board?
printBoard(mylist)?
1 2 3
4 5 6
987
[[1,2,3],[4,5,6],[7,8,9]]
Accessing an element
How do I get an element at row 0, column 2
getElement(board,row,column)?
1 2 3
4 5 6
987
[[1,2,3],[4,5,6],[7,8,9]]
Setting an element
How do I get an element at row 0, column 2
setElement(board,row,column,player)?
1 2 3
4 5 6
987
[[1,2,3],[4,5,6],[7,8,9]]
Winning
How do we know if a player won?
gameWon(board,player)?
1 2 3
4 5 6
987
[[1,2,3],[4,5,6],[7,8,9]]
Strings
Strings are immutable Once created, a string itself cannot be
changed You can assign a different string to an existing
variablesname = “Daun Chung"name.lower()print namename = name.lower()print name
String Functions
s.count(sub) – Count the number of occurrences of sub in s
name = “Daun Chuung"print name.count(“u")print name.count(“uu")
String Functions
s.find(sub) Find the first index of sub in s, or -1 if not found
name = “Yasser El-Sayed"print name.find(“El")print name.find(“ “)print name.find(“Yasser")print name.find(“yasser")
String Functions
s.index(sub) – find the first index of sub in s or error if not found
name = “Yasser El-Sayed"print name.find(“El")print name.find(“ “)print name.find(“Yasser")print name.find(“yasser")
String Functions
s.rfind(sub) – Find the last index of sub in s or -1 if not founds.rindex(sub) – Find the last index of sub
in s or Error if not found
String Functions
s.split() – Returns a list of words in s
name = “Muhammad Ali Najeeb Qazi"eachName = name.split()print eachName
OUTPUT [‘Muhammad', ‘Ali', ‘Najeeb‘, ‘Qazi’]
String Functions
s.join(lst) – Join the list of words into a single string using s as separator
a = ["Harry", "Potter","and","the","goblet","of","fire"]t = " "print t.join(a) t = “..”print t.join(a)
Harry Potter and the goblet of fire
Harry..Potter..and..the..goblet..of..fire
Working with Strings
Count the number of vowels in a stringa = raw_input(“Enter something> ”)s = a.lower()print s.count(‘a’) + s.count(‘e’) +
s.count(‘i’) + s.count(‘o’) +s.count(‘u’)