Homework 7 Solutions¶

Enter your name and EID here

This homework is due on Mar. 20, 2018 at 7:00pm. Please submit as a PDF file on Canvas. Before submission, please re-run all cells by clicking "Kernel" and selecting "Restart & Run All."

Problem 1 (5 pts): Create a list with the names of your favorite animals. Your list should contain at least 5 animals. Then, complete the following steps using python. Each step should be completed in a different cell and your results should be printed with print().

1. Sort the list so that the names appear in alphabetical order.
2. How many items are in the list? Hint: use the function len().
3. Remove an animal from your list and print the length of the list again.
5. Make a new list in which every animal name in your original list is repeated 3 times.
In [1]:
animals = ['zebra', 'polar bear', 'penguin', 'parrot', 'giraffe']

In [2]:
print(animals)
animals.sort()
print(animals)

['zebra', 'polar bear', 'penguin', 'parrot', 'giraffe']
['giraffe', 'parrot', 'penguin', 'polar bear', 'zebra']

In [3]:
print(len(animals))

5

In [4]:
animals.remove('parrot')
print(len(animals))

4

In [5]:
animals.append('ostrich')
print(animals)

['giraffe', 'penguin', 'polar bear', 'zebra', 'ostrich']

In [6]:
new_animals = animals * 3
print(new_animals)

['giraffe', 'penguin', 'polar bear', 'zebra', 'ostrich', 'giraffe', 'penguin', 'polar bear', 'zebra', 'ostrich', 'giraffe', 'penguin', 'polar bear', 'zebra', 'ostrich']


Problem 2 (5 pts): Now imagine that you have gone to the zoo to see your favorite animals listed in Problem 1. Create a dictionary that contains the names of the animals as keys and counts for each animal as the values. (For example, if there are 3 giraffes at the zoo, the key would be "giraffe" and the value would be 3.) The counts should just be made-up counts. Then, complete the following steps using python. Each step should be completed in a different cell and your results should be printed with print().

1. Write code that counts how many animals total you observed. (In other words, add up all of the animal counts.)
2. Add an animal "monkey" to your dictionary and give it a count of 5.
3. Print out a list of the keys and a list of the values in your dictionary (i.e., the animal names and the counts).
4. Change the count for one of the animals in your dictionary (i.e., monkey's count 5 to 8).
5. Remove a key-value pair from your dictionary.
In [7]:
animal_dict = {'zebra': 5, 'polar bear': 2, 'penguin': 7, 'parrot': 1, 'giraffe': 3}
print(animal_dict)

{'zebra': 5, 'polar bear': 2, 'penguin': 7, 'parrot': 1, 'giraffe': 3}

In [8]:
total = 0
for key in animal_dict.keys():
total += animal_dict[key]
print(total)

18

In [9]:
animal_dict['monkey'] = 5
print(animal_dict)

{'zebra': 5, 'polar bear': 2, 'penguin': 7, 'parrot': 1, 'giraffe': 3, 'monkey': 5}

In [10]:
print(animal_dict.keys())
print(animal_dict.values())

dict_keys(['zebra', 'polar bear', 'penguin', 'parrot', 'giraffe', 'monkey'])
dict_values([5, 2, 7, 1, 3, 5])

In [11]:
animal_dict['monkey'] = 8
print(animal_dict)

{'zebra': 5, 'polar bear': 2, 'penguin': 7, 'parrot': 1, 'giraffe': 3, 'monkey': 8}

In [12]:
del animal_dict['giraffe']
print(animal_dict)

{'zebra': 5, 'polar bear': 2, 'penguin': 7, 'parrot': 1, 'monkey': 8}