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.
Enter the following assignment statement:
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 references to mylist
.10
using only mathematical operators and references to mylist
.mylist
using only mathematical operators and references to mylist
.ourlist = [5, 6, 7, 8]
mylist
's value to [4, 5, 6, 7, 8]
, using only references to mylist
and ourlist
.In class, we said that in general, operations on the same types will output the same type. But what happens when you try to operate on two different types?
3 + 7.2
? What is the type of your answer?Notice that when you added an integer and a float (respectively), you received a float. Intuitively, it makes sense to us that you should be able to add integers and floats together. A nice feature of Python is that it allows you to perform mathematical operations on integers with floats, which usually makes our lives easier.
Side Note: Why does Python return a float when operating on integers with floats and not an integer instead? Floats are more "precise" and in that sense hold more information: imagine you're measuring something. Which measurement gives you more information: 3 or 3.4? So, if Python decided to return an integer, there would potentially be a loss of information.
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]
?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?Rename your program FirstLast_HW2-1.py
and share it with cs0931handinfall2015@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 nothing appears in the Shell, don't worry, as long as no red error messages appear. If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!