Mar 7, 2019
Complete the following tasks in python. You may do them either from the ipython command line (qt-console) or by creating code chunks within this jupyter notebook.
Perform the following basic operations in python. Note that you will have to print your result with the function print()
if you want to see the output in your ipython notebook.
# Your solutions go here. Create a separate code chunk for each question
# using the + icon in the toolbar.
# 1. Add 3 and 5
3+5 # without printing, nothing visible happens
print(3+5) # this generates output.
# 2. Multiply 3 and 5
print(3*5)
# 3. Assign 3 to variable 'a' and print
a = 3
print(a)
# 4. Assign 3 to variable 'a', 5 to variable 'b', product to 'c'
a = 3
b = 5
c = a*b
print(c)
Define a variable called mystring
, which contains a lengthy string of some kind (random letters, your address, song lyrics, a haiku, whatever). Perform the following tasks with this variable:
complete_string
which contains the phrase "Here is my string: " followed by the contents of your mystring
variable. (Hint - use the +
operator). For example, if your mystring
variable contains the text "Time flies like an arrow; fruit flies like a banana.", then complete_string
should read, "Here is my string: Time flies like an arrow; fruit flies like a banana."mystring
with the letter "X". How did that go?.split()
method to split your string into a list.mystring
, replace the all occurrences of a letter of your choice with a number. Which string method can achieve this goal?# Your solutions go here. Create a separate code chunk for each question
# using the + icon in the toolbar.
# 1. Make variable and print
mystring = "Time flies like an arrow; fruit flies like a banana."
print(mystring)
# 2. Make variable 'complete_string'
complete_string = "Here is my string: " + mystring
print(complete_string)
# 3. Select pieces of string through indexing
# first letter (remember, python counts from 0)
selection = mystring[0]
print(selection)
# first five letters
selection = mystring[0:5]
print(selection)
# last seven letters
selection = mystring[-7:]
print(selection)
# 4. Try to replace character through indexing
# mystring[4] = 'X' # does not work, python strings cannot be changed in place
# 5. Split the string
mystring.split()
# 6. Replace letter 'i' with '1' using the `.replace()` function
mystring.replace('i', '1')
Define a list variable called animals
which contains the following six entries: "monkey", "giraffe", "shark", "caterpillar", "squid", and "jellyfish". Perform the following tasks with this variable:
.append()
to add "spider" to the end of the list.in
to determine if "dog" is in the animals
list. Also try with "monkey"..join()
method to create a string which contains each animal in the animals
list separated by the string "; " (semicolon and a space). Read the documentation of .join()
or google it to solve this problem. Hint: .join()
is a string function that takes a list as its argument.# Your solutions go here. Create a separate code chunk for each question
# using the + icon in the toolbar.
# 1. Make list, select first animal
animals = ["monkey", "giraffe", "shark", "caterpillar", "squid", "jellyfish"]
animals[0]
# 2. Middle two animals (remember, python counts from 0, and we need to
# index one past the last element we want to select)
animals[2:4]
# 3. Last animal
animals[-1]
# 4. Change second animal to cat
animals[1] = "cat"
print(animals)
# 5. Append "spider" to the list
animals.append("spider")
print(animals)
# 6. Check if "dog" is in the list
"dog" in animals
# check if "monkey" is in the list
"monkey" in animals
# 7. Join the strings in the list together
"; ".join(animals)
Create a dictionary called molecules
with the following three key-value pairs: "hair" -"keratin"; "DNA" - "nucleotides"; "protein" - "amino acids". Perform the following tasks with this variable:
molecules
dictionary. Then, add another key-value pair, "RNA" - "nucleotides". Did this work? What does this tell you about the allowed uniqueness for dictionary keys and values?molecules
dictionary. What happened? Can you see why? .keys()
and .values()
.zip()
function for this. (You may need to look up this function). Save this list of tuples to a variable. Then re-cast the list of tuples back into a dictionary.# Your solutions go here. Create a separate code chunk for each question
# using the + icon in the toolbar.
# 1. Make dictionary and print it out
molecules = {"hair":"keratin", "DNA":"nucleotides", "protein":"amino acids"}
print(molecules)
# 2. Indexing goes by key, not by number
# molecules[1] # causes an error
molecules["DNA"] # this works
# 3. Adding key-value pairs
molecules["ribosomes"] = "RNA" # this adds a new key "ribosomes"
print(molecules)
# The following adds a new key "RNA". It doesn't matter that "RNA" was
# already used as a value.
molecules["RNA"] = "nucleotides"
print(molecules)
# 4. Add yet another key-value pair, "ribosomes"-"rRNA"
# This overwrites the previous "ribosomes" entry, keys need to be unique
molecules["ribosomes"] = "rRNA"
print(molecules)
# 5. Extract keys and values from dictionary
print(molecules.keys())
print(molecules.values())
# 6. Turn dictionary into list of tuples, and then back into dictionary
# First, create list of keys and values
# We need the `list()` function to actually create a list we can print
keys_values = list(zip(molecules.keys(), molecules.values()))
print(keys_values)
# Back to dictionary
dict(keys_values)