# 01 Introduction to Lists
zoo_animals = ["pangolin", "cassowary", "sloth", "panda"];
# One animal is missing!
if len(zoo_animals) > 3:
print ("The first animal at the zoo is the " + zoo_animals[0])
print ("The second animal at the zoo is the " + zoo_animals[1])
print ("The third animal at the zoo is the " + zoo_animals[2])
print ("The fourth animal at the zoo is the " + zoo_animals[3])
The first animal at the zoo is the pangolin The second animal at the zoo is the cassowary The third animal at the zoo is the sloth The fourth animal at the zoo is the panda
# 02 Access by Index
numbers = [5, 6, 7, 8]
print ("Adding the numbers at indices 0 and 2...")
print (numbers[0] + numbers[2])
# Your code here!
print ("Adding the numbers at indices 1 and 3...")
print (numbers[1] + numbers[3])
Adding the numbers at indices 0 and 2... 12 Adding the numbers at indices 1 and 3... 14
# 03 New Neighbors
zoo_animals = ["pangolin", "cassowary", "sloth", "tiger"]
# Last night our zoo's sloth brutally attacked
#the poor tiger and ate it whole.
# The ferocious sloth has been replaced by a friendly hyena.
zoo_animals[2] = "hyena"
# What shall fill the void left by our dear departed tiger?
# Your code here!
zoo_animals[3] = "panda"
# 04 Late Arrivals and List Length
suitcase = []
suitcase.append("sunglasses")
# Your code here!
suitcase.append("shirt")
suitcase.append("trousers")
suitcase.append("coat")
list_length = len(suitcase)# Set this to the length of suitcase
print ("There are %d items in the suitcase." % (list_length))
print (suitcase)
There are 4 items in the suitcase. ['sunglasses', 'shirt', 'trousers', 'coat']
# 05 List Slicing
suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]
first = suitcase[:2] # same as suitcase[0: 2] # The first two items
middle = suitcase[2:4] # Third and fourth items
last = suitcase[len(suitcase) - 2:] # same as suitcase[len(suitcase) - 2: len(suitcase)]
# The last two items
print (first)
print (middle)
print (last)
['sunglasses', 'hat'] ['passport', 'laptop'] ['suit', 'shoes']
# 06 Slicing Lists_and Strings
animals = "catdogfrog"
cat = animals[:3] # The first three characters of animals
dog = animals[3:6] # The fourth through sixth characters
frog = animals[6:] # From the seventh character to the end
print (cat)
print (dog)
print (frog)
cat dog frog
# 07 Maintaining Order
animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck") # Use index() to find "duck"
print(duck_index)
2
# Your code here!
animals.insert(duck_index, "cobra")
print (animals) # Observe what prints after the insert operation
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fennec fox']
# 08 For One and All
my_list = [1,9,3,8,5,7]
for number in my_list:
# Your code here
print (number * 2)
2 18 6 16 10 14
# 09 More with for
start_list = [5, 3, 1, 2, 4]
square_list = []
# Your code here!
for e in start_list:
square_list.append(e ** 2)
square_list.sort()
print (square_list)
[1, 4, 9, 16, 25]
# 10 This Next Part is Key
# 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
# Your code here!
print (residents["Sloth"])
print (residents["Burmese Python"])
104 105 106
# 11 New Entries
menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
print (menu['Chicken Alfredo'])
# Your code here: Add some dish-price pairs to menu!
menu['Beef'] = 50.00
menu['pork'] = 20.00
menu['porridge'] = 5.00
print ("There are " + str(len(menu)) + " items on the menu.")
print (menu)
14.5 There are 4 items on the menu. {'Beef': 50.0, 'Chicken Alfredo': 14.5, 'pork': 20.0, 'porridge': 5.0}
# 12 Changing Your Mind
# 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'] = 'What?'
print (zoo_animals)
{'Atlantic Puffin': 'Arctic Exhibit', 'Rockhopper Penguin': 'What?'}
# 13 It js Dangerous to Go Alone Take This
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()
# Here the dictionary access expression takes the place of a list name
# Your code here
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
inventory['gold'] += 50
inventory
{'backpack': ['bedroll', 'bread loaf', 'xylophone'], 'burlap bag': ['apple', 'small ruby', 'three-toed sloth'], 'gold': 550, 'pocket': ['seashell', 'strange berry', 'lint'], 'pouch': ['flint', 'gemstone', 'twine']}