CSCI 0931

Intro Comp for Humanities & Social Sciences

Alexandra Papoutsaki

Due October 6, 2015, 9:00 am

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.

If a problem is marked as “**(Independent)**”, you may only discuss the problem with course staff. Otherwise, you are free to discuss the concepts that will help you solve the problems with classmates as well as course staff. However, you are **never** allowed to simply copy answers.

Enter the following assignment statement:

mylist = [4, 18, 13, 7]

- 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. - Write an expression that evaluates to
`20`

using only mathematical operators (`+`

,`-`

,`*`

,`/`

,`(`

,`)`

) and references to`mylist`

. - Write an expression that evaluates to
`10`

using only mathematical operators and references to`mylist`

. - 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`

. - Now assign the following:
Change
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?

- What does Python output when you evaluate
`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]

- What would Python output if you told it to evaluate the expression
`stringlist[1]`

? - What about the expression
`stringlist[1][:7]`

(this one is a bit trickier)? - 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`

. - 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 `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!**