Introduction to Python - Lecture 6
Zohair ul Hasan
Yesterday's homework
Dictionaries
Like a list, a dictionary is a mutable collection of many values
But unlike indexes for lists, indexes for dictionaries can use many different data types, not just integers
Indexes for dictionaries are called keys, and a key with its associated value is called a key-value pair.
>>> myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
>>> myCat['size']
'fat'
>>> 'My cat has ' + myCat['color'] + ' fur.'
'My cat has gray fur.'
>>> spam = {12345: 'Luggage Combination', 42: 'The Answer'}>>> spam = ['cats', 'dogs', 'moose']
>>> bacon = ['dogs', 'moose', 'cats']
>>> spam == bacon
False
>>> eggs = {'name': 'Zophie', 'species': 'cat', 'age': '8'}
>>> ham = {'species': 'cat', 'age': '8', 'name': 'Zophie'}
>>> eggs == ham
True>>> spam = {'name': 'Zophie', 'age': 7}
>>> spam['color']
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
spam['color']
KeyError: 'color'birthdays = {'Alice': 'Apr 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
while True:
print('Enter a name: (blank to quit)')
name = input()
if name == '':
break
if name in birthdays:
print(birthdays[name] + ' is the birthday of ' + name)
else:
print('I do not have birthday information for ' + name)
print('What is their birthday?')
bday = input()
birthdays[name] = bday
print('Birthday database updated.')Enter a name: (blank to quit)
Alice
Apr 1 is the birthday of Alice
Enter a name: (blank to quit)
Eve
I do not have birthday information for Eve
What is their birthday?
Dec 5
Birthday database updated.
Enter a name: (blank to quit)
Eve
Dec 5 is the birthday of Eve
Enter a name: (blank to quit)keys(), values(), and items() Methods>>> myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
>>> type(myCat.keys())
<class 'dict_keys'>
>>> type(myCat.values())
<class 'dict_values'>
>>> type(myCat.items())
<class 'dict_items'>
>>> spam = {'color': 'red', 'age': 42}
>>> for v in spam.values():
... print(v)
red
42keys(), values(), and items() Methods>>> for k in spam.keys():
... print(k)
color
age
>>> for i in spam.items():
... print(i)
('color', 'red')
('age', 42)keys(), values(), and items() Methods>>> spam = {'color': 'red', 'age': 42}
>>> spam.keys()
dict_keys(['color', 'age'])
>>> list(spam.keys())
['color', 'age']
keys(), values(), and items() Methods>>> spam = {'color': 'red', 'age': 42}
>>> for k, v in spam.items():
... print('Key: ' + k + ' Value: ' + str(v))
Key: age Value: 42
Key: color Value: red>>> spam = {'name': 'Zophie', 'age': 7}
>>> 'name' in spam.keys()
True
>>> 'Zophie' in spam.values()
True
>>> 'color' in spam.keys()
False
>>> 'color' not in spam.keys()
True
>>> 'color' in spam
False>>> picnicItems = {'apples': 5, 'cups': 2}
>>> 'I am bringing ' + str(picnicItems.get('cups', 0)) + ' cups.'
'I am bringing 2 cups.'
>>> 'I am bringing ' + str(picnicItems.get('eggs', 0)) + ' eggs.'
'I am bringing 0 eggs.'>>> picnicItems = {'apples': 5, 'cups': 2}
>>> 'I am bringing ' + str(picnicItems['eggs']) + ' eggs.'
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
'I am bringing ' + str(picnicItems['eggs']) + ' eggs.'
KeyError: 'eggs'>>> import pprint
>>> my_dict = { "key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4", "key5": "value5", "key6": "value6", "key7": "value7", "key8": "value8", "key9": "value9"}
>>> print(my_dict)
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6': 'value6', 'key7': 'value7', 'key8': 'value8', 'key9': 'value9'}
>>> pprint.pprint(my_dict)
{'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
'key4': 'value4',
'key5': 'value5',
'key6': 'value6',
'key7': 'value7',
'key8': 'value8',
'key9': 'value9'}theBoard = {
"top-L": " ",
"top-M": " ",
"top-R": " ",
"mid-L": " ",
"mid-M": " ",
"mid-R": " ",
"low-L": " ",
"low-M": " ",
"low-R": " ",
}theBoard = {
"top-L": " ",
"top-M": " ",
"top-R": " ",
"mid-L": " ",
"mid-M": "X",
"mid-R": " ",
"low-L": " ",
"low-M": " ",
"low-R": " ",
}
theBoard = {
"top-L": "O",
"top-M": "O",
"top-R": "O",
"mid-L": "X",
"mid-M": "X",
"mid-R": " ",
"low-L": " ",
"low-M": " ",
"low-R": "X",
}
theBoard = {
"top-L": " ",
"top-M": " ",
"top-R": " ",
"mid-L": " ",
"mid-M": " ",
"mid-R": " ",
"low-L": " ",
"low-M": " ",
"low-R": " ",
}
def printBoard(board):
print(board["top-L"] + "|" + board["top-M"] + "|" + board["top-R"])
print("-+-+-")
print(board["mid-L"] + "|" + board["mid-M"] + "|" + board["mid-R"])
print("-+-+-")
print(board["low-L"] + "|" + board["low-M"] + "|" + board["low-R"])
printBoard(theBoard)
| |
-+-+-
| |
-+-+-
| |theBoard = {
"top-L": "O",
"top-M": "O",
"top-R": "O",
"mid-L": "X",
"mid-M": "X",
"mid-R": " ",
"low-L": " ",
"low-M": " ",
"low-R": "X",
}
def printBoard(board):
print(board["top-L"] + "|" + board["top-M"] + "|" + board["top-R"])
print("-+-+-")
print(board["mid-L"] + "|" + board["mid-M"] + "|" + board["mid-R"])
print("-+-+-")
print(board["low-L"] + "|" + board["low-M"] + "|" + board["low-R"])
printBoard(theBoard)
O|O|O
-+-+-
X|X|
-+-+-
| |XtheBoard = {
"top-L": " ",
"top-M": " ",
"top-R": " ",
"mid-L": " ",
"mid-M": " ",
"mid-R": " ",
"low-L": " ",
"low-M": " ",
"low-R": " ",
}
def printBoard(board):
print(board["top-L"] + "|" + board["top-M"] + "|" + board["top-R"])
print("-+-+-")
print(board["mid-L"] + "|" + board["mid-M"] + "|" + board["mid-R"])
print("-+-+-")
print(board["low-L"] + "|" + board["low-M"] + "|" + board["low-R"])
turn = "X"
for i in range(9):
printBoard(theBoard)
print("Turn for " + turn + ". Move on which space?")
move = input()
theBoard[move] = turn
if turn == "X":
turn = "O"
else:
turn = "X"
printBoard(theBoard)
theBoard = {
"top-L": " ",
"top-M": " ",
"top-R": " ",
"mid-L": " ",
"mid-M": " ",
"mid-R": " ",
"low-L": " ",
"low-M": " ",
"low-R": " ",
}
def printBoard(board):
print(board["top-L"] + "|" + board["top-M"] + "|" + board["top-R"])
print("-+-+-")
print(board["mid-L"] + "|" + board["mid-M"] + "|" + board["mid-R"])
print("-+-+-")
print(board["low-L"] + "|" + board["low-M"] + "|" + board["low-R"])
turn = "X"
for i in range(9):
printBoard(theBoard)
print("Turn for " + turn + ". Move on which space?")
move = input()
theBoard[move] = turn
if turn == "X":
turn = "O"
else:
turn = "X"
printBoard(theBoard)
| |
-+-+-
| |
-+-+-
| |
Turn for X. Move on which space?
mid-M
| |
-+-+-
|X|
-+-+-
| |
--snip--
O|O|X
-+-+-
X|X|O
-+-+-
O| |X
Turn for X. Move on which space?
low-M
O|O|X
-+-+-
X|X|O
-+-+-
O|X|XallGuests = {
"Alice": {"apples": 5, "pretzels": 12},
"Bob": {"ham sandwiches": 3, "apples": 2},
"Carol": {"cups": 3, "apple pies": 1},
}
def totalBrought(guests, item):
numBrought = 0
for k, v in guests.items():
numBrought = numBrought + v.get(item, 0)
return numBrought
print("Number of things being brought:")
print(" - Apples " + str(totalBrought(allGuests, "apples")))
print(" - Cups " + str(totalBrought(allGuests, "cups")))
print(" - Cakes " + str(totalBrought(allGuests, "cakes")))
print(" - Ham Sandwiches " + str(totalBrought(allGuests, "ham sandwiches")))
print(" - Apple Pies " + str(totalBrought(allGuests, "apple pies")))
Number of things being brought:
- Apples 7
- Cups 3
- Cakes 0
- Ham Sandwiches 3
- Apple Pies 1Complete the activity at https://itp.zohair.dev/activities/fantasy-game-inventory