In-class worksheet 14

Mar 1, 2018

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.

1. Working with numbers

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.

  1. Add two numbers
  2. Multiply two numbers
  3. Assign a number to a variable and then print the variable
  4. Assign two numbers to two different variables, then assign the product of those two variables to a third. Print the third variable.
In [1]:
# Your solutions go here. Create a separate code chunk for each question
# using the + icon in the toolbar.

2. Working with strings

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:

  1. Print the contents of your variable.
  2. Create a new variable called 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."
  3. Use indexing to select the first letter of your string, the first five letters, or the last seven letters.
  4. Attempt to use indexing to replace the character in the 5th position of mystring with the letter "X". How did that go?
  5. Use the .split() method to split your string into a list.
  6. In your variable mystring, replace the all occurrences of a letter of your choice with a number. Which string method can achieve this goal?
In [2]:
# Your solutions go here. Create a separate code chunk for each question
# using the + icon in the toolbar.

3. Working with lists

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:

  1. Select the first animal in the list.
  2. Select the middle two animals (numbers 3 and 4) in the list.
  3. Select the last animal in the list.
  4. Use indexing to change the second animal in the list to "cat".
  5. Use the list method .append() to add "spider" to the end of the list.
  6. Use the operator in to determine if "dog" is in the animals list. Also try with "monkey".
  7. Use the .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.
In [3]:
# Your solutions go here. Create a separate code chunk for each question
# using the + icon in the toolbar.

4. Working with dictionaries

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:

  1. Print out the dictionary and examine the order of the key-value pairs. Is this what you expected?
  2. Try to use indexing to select the second key-value pair from the dictionary. Does this work? Now try to index the value for "DNA".
  3. Add another key-value pair, "ribosomes"-"RNA", to the 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?
  4. Now, add yet another key-value pair, "ribosomes"-"rRNA" to the molecules dictionary. What happened? Can you see why?
  5. Print out just the keys and just the values of the dictionary, using the dictionary methods .keys() and .values().
  6. Create a list of tuples, which in each tuple is the (key, value) pair. Use the 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.
In [4]:
# Your solutions go here. Create a separate code chunk for each question
# using the + icon in the toolbar.