Mar 5, 2020
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.
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.
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.
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.