Homework 2-1

Due February 26, 2013, 2:25 pm

For this assignment you will first figure out your answers in the Python shell. When you feel like you've arrived at the right answers, type them up in a single Python file and hand it in to the TAs. For some guidance on how to do this, see the Python homework instructions.


For the following problems you may discuss the concepts that will help solve these problems with classmates and course staff. You may not simply copy down the answers of your classmates as that is a violation of the collaboration policy. The one exception to this rule are those problems marked as “(Independent)”. You may discuss independent problems with course staff only.

Task 1

Enter the following assignment statement:

mylist = [4, 18, 13, 7]
  1. Evaluate the expressions mylist[1] and mylist[2]. Are they what you expected them to be? Remember that indices of a list start at 0, not 1.
  2. Write an expression that evaluates to 20 using only mathematical operators (+,-,*,/,(,)) and references to mylist.
  3. Write an expression that evaluates to 10 using only mathematical operators and references to mylist.
  4. Write a series of statements and expressions to find the average value of the integers (as an integer) in mylist using only mathematical operators and references to mylist. Note that your answer is an integer, so it may not be exactly the precise numerical value you'd expect — we'll explore this further in Task 2.
  5. Now assign the following:
    ourlist = [5, 6, 7, 8]
    Change mylist's value to [4, 5, 6, 7, 8], using only references to mylist and ourlist.

Task 2

Look at the following code:

numOfBertsToys = 10
stuffedAnimals = ['muffins', 'bonkers', 'grumpy', 'ed']
numberOfStuffies = len(stuffedAnimals)
answer = numberOfStuffies / numOfBertsToys
  1. What does the variable answer represent in terms of Bert's stuffed animals and his total number of toys?
  2. Try running the code, and then ask Python to evalute the value of answer. Why is it 0? What would you change to get the correct value?
  3. How would your rewrite your answer to question 1.4 to give you the correct answer as a decimal?

Task 3

Consider the following assignment statements:

string1 = "Cookie Monster "
string2 = "is hungry for a cookie. "
string3 = "We'd better go to Meeting Street Cafe."
stringlist = [string1, string2, string3]
  1. What would Python output if you told it to evaluate the expression stringlist[1]?
  2. What about the expression stringlist[1][:7] (this one is a bit trickier)?
  3. Using only references to stringlist and the + operator, write an expression that gives you two full sentences using the words in string1, string2, and string3.
  4. Using only references to string1 and string2, indexing, and the + operator, write an expression that evaluates to "Cookie Monster is hungry for cookies.". Hint: how do you use indexing to pull out only one word, or only one letter, from a string?


Rename your program YourName_HW2-1.py and email it to cs0931handin@cs.brown.edu.