Homework 2-1

Adapted from Brown University CSCI0931. Used with Permission.

For this assignment we suggest that you 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 program and hand it in to the TAs. For some guidance on how to do this, see the Python homework instructions.

Note: Your final handin should be a program FirstLast_StudentID_HW2-1.py and handed in to us. (Some students did not read through the whole assignment first, and did not realize till the end of the assignment that they needed to hand this in!)


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 statements and print out the answer.

mylist = [4, 18, 13, 7]
  1. Evaluate (e.g. print out) 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 stuff inside mylist.
  3. Write an expression that evaluates to 10 using only mathematical operators and stuff inside mylist.
  4. Write a series of statements and expressions to find the average value of the numbers in mylist using only mathematical operators and references to mylist.
  5. Now assign the following:
    ourlist = [5, 6, 7, 8]
    Change mylist's value to [9, 24, 20, 15], using only stuff inside mylist and ourlist.

Task 2

Look at the following code:

numOfBertsToys = 10
stuffedAnimals = ['muffins', 'bonkers', 'grumpy', 'ed']
numberOfStuffies = len(stuffedAnimals)
  1. Finish the code to get Python to print out the percentage of Bert's toys which are stuffed animals. Hint: to change a number to a string, you can use str() (e.g. str(4) will give you '4').

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]? Write a statement to print out your answer.
  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?

Task 4

Write programming statements that will convert the temperature from Celsius to Fahrenheit. You must use a variable to store the temperature in Celsius (just pick some number and assign it to the variable), and another variable to store the temperature in Fahrenheit. You must print out the original temperature (in Celsius) and the converted temperature (in Fahrenheit) in one single sentence. You can decide the names of the variables and the format of the output string on your own. The conversion formula is: Fahrenheit = Celsius × 9 ÷ 5 + 32


Rename your program FirstLast_StudentID_HW2-1.py and share it with PolyUCOMP1D04@gmail.com.

Note: Before you turn in your Python files, make sure they run without any errors. (Save your Python file. Then select Run > Run Module or hit F5 on your keyboard)! If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!