Solutions for python in class exercise

Tab a

Let's practice with lists first. One way to explore data structures is to learn their methods. Check all the methods of a list by running 'dir()' on a list object. Let's explore these functions using the following list object, by answering the below questions. See here for list methods:

In [16]:
list_exercise = ["Ramy", "Victorie", "Letty", "Robin", "Antoine", "Griffin"] 
  1. Add "Cathy O'Neil" to the list. Insert "Professor Crast" as the first element of the list
In [4]:
list_exercise.append("Cathy O'neil")
list_exercise + ["Cathy Oneil"]
# a
Out[4]:
['Ramy',
 'Victorie',
 'Letty',
 'Robin',
 'Antoine',
 'Griffin',
 "Cathy O'neil",
 'Cathy Oneil']
In [7]:
help(list_exercise.__add__)
Help on method-wrapper:

__add__(value, /)
    Return self+value.

In [3]:
# insert first position
["Professor Crast"] + list_exercise 
list_exercise.insert(0, "Professor Crast")
print(list_exercise)
['Professor Crast', 'Ramy', 'Victorie', 'Letty', 'Robin', 'Antoine', 'Griffin']
  1. Remove "Letty" from the list. Also remove the last element of the list.
In [8]:
# 1
list_exercise.remove("Letty")
print(list_exercise)
['Professor Crast', 'Ramy', 'Victorie', 'Robin', 'Antoine', 'Griffin']
In [17]:
# 2
list_exercise.pop()
print(list_exercise)
['Ramy', 'Victorie', 'Letty', 'Robin', 'Antoine']
  1. Find the index of the occurrence of the name "Robin". Count the number of times None appears in the list.
In [18]:
# solution
index_robin = list_exercise.index("Robin")
print(index_robin)
3
In [19]:
# solution
list_exercise.count(None)
Out[19]:
0
  1. Create a new list with the names in alphabetical order, copy this list as a new list without changing the values of the original list
In [20]:
list_copy = list_exercise.copy()
list_copy.sort()

# notice the modification in place
print(list_copy)
print(list_exercise)
['Antoine', 'Letty', 'Ramy', 'Robin', 'Victorie']
['Ramy', 'Victorie', 'Letty', 'Robin', 'Antoine']
  1. Add the string "Lovell" to copied_list and ensure that list_exercise remains unchanged.
In [21]:
list_copy.append("Lovell")
print(list_copy)
print(list_exercise)
['Antoine', 'Letty', 'Ramy', 'Robin', 'Victorie', 'Lovell']
['Ramy', 'Victorie', 'Letty', 'Robin', 'Antoine']

Tab B

Let's do a similar exercise with Dictionaries. Consider the dictionary below. See here for dictionary methods:

In [28]:
dict_exercise = {"Ramy": "India",
                  "Victorie":"Haiti", 
                  "Letty":"England", 
                  "Robin":"Canton", 
                  "Antoine":"Nigeria", 
                  "Griffin":"China"}
dict_exercise
Out[28]:
{'Ramy': 'India',
 'Victorie': 'Haiti',
 'Letty': 'England',
 'Robin': 'Canton',
 'Antoine': 'Nigeria',
 'Griffin': 'China'}
  1. Look up the keys in the dictionary, and store them in a list object called keys
In [23]:
keys = list(dict_exercise.keys())
print(keys)
['Ramy', 'Victorie', 'Letty', 'Robin', 'Antoine', 'Griffin']
  1. Add yourself, and two other colleagues in this dictionary. The values are the countries the person in the key was born.
In [25]:
dict_exercise["tiago"]="Brazil"
print(dict_exercise)
{'Ramy': 'India', 'Victorie': 'Haiti', 'Letty': 'England', 'Robin': 'Canton', 'Antoine': 'Nigeria', 'Griffin': 'China', 'tiago': 'Brazil'}
  1. Remove "Ramy" from the dictionary, and save as another dictionary
In [30]:
# create new
dict_new={"Ramy":dict_exercise.get("Ramy")}
print(dict_new)
{'Ramy': None}
In [ ]:
# pop
dict_exercise.pop("Ramy")
print(dict_exercise)
In [29]:
# del
del dict_exercise["Victorie"]
print(dict_exercise)
{'Victorie': 'Haiti', 'Letty': 'England', 'Robin': 'Canton', 'Antoine': 'Nigeria', 'Griffin': 'China'}
{'Letty': 'England', 'Robin': 'Canton', 'Antoine': 'Nigeria', 'Griffin': 'China'}

Tab C

Let's now play around with some string methods. See the string below from the book "Babel:An Arcane History". See here for string methods:

In [29]:
babel = """
That's just what translation is, I think. That's all speaking is. 
Listening to the other and trying to see past your own biases to glimpse what they're trying to say. 
Showing yourself to the world, and hoping someone else understands."
"""
  1. Determine if the word "Babel" is present in the string.
In [30]:
"Babel" in babel
Out[30]:
False
  1. Count how many times the word "translation" appears
In [31]:
# count
babel.count("translation")
Out[31]:
1
In [32]:
# where
babel.index("translation")
Out[32]:
18
In [34]:
# see
babel[17:18+len("translation")]
Out[34]:
' translation'
  1. Convert the entire string to upper case
In [35]:
babel.upper()
Out[35]:
'\nTHAT\'S JUST WHAT TRANSLATION IS, I THINK. THAT\'S ALL SPEAKING IS. \nLISTENING TO THE OTHER AND TRYING TO SEE PAST YOUR OWN BIASES TO GLIMPSE WHAT THEY\'RE TRYING TO SAY. \nSHOWING YOURSELF TO THE WORLD, AND HOPING SOMEONE ELSE UNDERSTANDS."\n'
  1. Convert the pronoum "I" to "We" in the entire text.
In [36]:
babel.replace("I ", "We ")
Out[36]:
'\nThat\'s just what translation is, We think. That\'s all speaking is. \nListening to the other and trying to see past your own biases to glimpse what they\'re trying to say. \nShowing yourself to the world, and hoping someone else understands."\n'
  1. Strip any punctuation (like commas, exclamation marks, etc.) from the string.
In [37]:
# Define a string containing some punctuation marks.
punctuation = ",'.!"

# Iterate over: we will see this later. 
for char in punctuation:
    babel = babel.replace(char, "")
    
print(babel)    
Thats just what translation is I think Thats all speaking is 
Listening to the other and trying to see past your own biases to glimpse what theyre trying to say 
Showing yourself to the world and hoping someone else understands"

In [24]:
!jupyter nbconvert _solution_inclass_python_1.ipynb --to html --template classic
[NbConvertApp] Converting notebook _solution_inclass_python_1.ipynb to html