Dictionaries
# Assigning a dictionary with three key-value pairs to residents:residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}
print residents['Puffin'] # Prints Puffin's room number
print residents['Sloth']
print residents['Burmese Python']
menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
print menu['Chicken Alfredo']
menu['Juice'] = 4.50
menu['Rice'] = 4.50
menu['Cake'] = 4.50
print "There are " + str(len(menu)) + " items on the menu."
print menu # key - animal_name : value - location
zoo_animals = { 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Arctic Exhibit'}
# A dictionary (or list) declaration may break across multiple lines
# Removing the 'Unicorn' entry. (Unicorns are incredibly expensive.)
del zoo_animals['Unicorn']
# Your code here!
del zoo_animals['Sloth']
del zoo_animals['Bengal Tiger']
zoo_animals['Rockhopper Penguin'] = 'Taiwan'
print zoo_animals inventory = {
'gold' : 500,
'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}
# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']
# Sorting the list found under the key 'pouch'
inventory['pouch'].sort()
# Your code here
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
inventory['gold'] += 50 prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
} prices = {
"banana" : 4,
"apple": 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple": 0,
"orange" : 32,
"pear" : 15,
}
total = 0
for key in prices:
print key
print "price: %s" % prices
print "stock: %s" % stock
sub_total = prices * stock
print "sub total: %s" % sub_total
total = total + sub_total
print total
用水果的價格及數量算出總金額 lloyd = {
"name": "Lloyd",
"homework": ,
"quizzes": ,
"tests":
}
alice = {
"name": "Alice",
"homework": ,
"quizzes": ,
"tests":
}
tyler = {
"name": "Tyler",
"homework": ,
"quizzes": ,
"tests":
}
students =
for x in students:
print x["name"]
for y in students:
print y["homework"]
for z in students:
print z["quizzes"]
for o in students:
print o["tests"]
頁:
[1]