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!)
Enter the following assignment statements and print out the answer.
mylist = [4, 18, 13, 7]
mylist[1]
and mylist[2]
. Are they what you expected them to be? Remember that indices of a list start at 0, not 1.20
using only mathematical operators (+
,-
,*
,/
,(
,)
) and stuff inside mylist
.10
using only mathematical operators and stuff inside mylist
.mylist
using only mathematical operators and references to mylist
. ourlist = [5, 6, 7, 8]
mylist
's value to [9, 24, 20, 15]
, using only stuff inside mylist
and ourlist
.Look at the following code:
numOfBertsToys = 10 stuffedAnimals = ['muffins', 'bonkers', 'grumpy', 'ed'] numberOfStuffies = len(stuffedAnimals)
str()
(e.g. str(4)
will give you '4'
).
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]
stringlist[1]
? Write a statement to print out your answer.stringlist[1][:7]
(this one is a bit trickier)?stringlist
and the +
operator, write an expression that gives you two full sentences using the words in string1
, string2
, and string3
.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?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!