1 h02 CS8 W19
Name:
(as it would appear on official course roster)
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

# h02: Perkovic 2.1 thru 2.5

true Mon 01/14 09:30AM Wed 01/23 09:30AM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the one lowest scores (if you have zeros, those are the one lowest scores.)

## Note that this homework is due on Wednesday, Jan. 23rd, since we have Jan. 21st off for MLK Day

1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts () in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
2. Section 2.1 describes how several operators and built in functions in Python work. What would be the result of entering the following at the Python interactive shell prompt?

(Note: You are encouraged to check your answers at the Python prompt before turning in your work, but try this on paper first, just by reading the text and trying to predict what will happen. Then try typing in the results at the Python prompt. Change your answers if they were mistaken, but even more important, try to figure out why you were incorrect.)

Be very precise. Note that True is not the same in Python as true; upper vs. lower case matters. You will not get full credit for answers that are not precisely correct.

Points Expression Result Points Expression Result
(4 pts) 2 + 3 * 5   (4 pts) 4 < 3
(4 pts) 19 % 3   (4 pts) 1 + 2 == 3
(4 pts) 5 ** 2   (4 pts) True and False
(4 pts) 7 // 2   (4 pts) True or False
(4 pts) 9 // 2   (4 pts) 5 != 10//2
3. (5 pts) As described in section 2.1, a Python assignment statement contains the assignment operator, an expression and a variable, but not in that order.

What is the correct order for these three parts, reading from left to right?

4. (5 pts) Section 2.1 contains a list of thirty-three reserved words in Python that may not be used as the name of a variable. You don’t need to memorize this list, but you do need to know where to find it, either in the book, or online. So, to be sure you can find it, list all of the Python reserved words that start with the letter c or f.

5. Section 2.2 describes strings in Python, including the concepts of “dictionary order” (also called “lexicographic order”), concatenation of strings, multiplication of strings times an integer, the in operator, the not in operation, the len function, and string indexing. Review that material.

Then, assuming the following assignment statements have been entered at the Python prompt:

school = "UCSB"
course = "CS8"
qtr = "M17"

6. Indicate the value of each of these expressions:

Expression Result     Expression Result
(4 pts) school * 2     (4 pts) school > course
(4 pts) qtr[1:3]     (4 pts) qtr < school
(4 pts) course[0:2]     (4 pts) len(qtr) > 4
(4 pts) 'D' in school     (4 pts) school[-1]
(4 pts) 'E' not in school     (4 pts) school[0]
7. As discussed in Section 2.4, the type() function returns the type of a Python value. When you pass a variable such as x, type(x) returns the type of the value that the variable x currently refers to.

Assume that the following assignment statement has been executed:

schools=["UCSB","Stanford","UCSD","Cal Poly"]


What will each of the expressions below evaluate to? As a reminder, strictly speaking, Python will print types in the format <class 'int'>, <class 'float'>, <class 'str'>, etc. so please use exactly that format for full credit.

Points Expression Result Points Expression Result
(4 pts) type(3)   (4 pts) type(1+2.5)
(4 pts) type('3')   (4 pts) type(2 * "3")
(4 pts) type("3.5")   (4 pts) type((3,3))
(4 pts) type(3.5)   (4 pts) type(schools)
(4 pts) type([3,5])   (4 pts) type(schools[0])
8. (10 pts) Assume that cases is the name of a variable with a float value that you want to convert to an integer value. Write a Python expression that converts cases to an integer (throwing away any fractional part)

9. (10 pts) Assume that courseNum is an integer that represents the numeric part of a course number (e.g. 3, 8, 130, 16, 24). Write a Python expression that converts courseNum to an string (i.e. <class 'str'> in Python)

10. (10 pts) If you want to check whether x is greater than 10, and y is greater than 5, you can write the Python expression

(x > 10) and (y > 5)


If x has the value 20, and y has the value 17, this evaluates to True.

But what if we accidentally wrote it as:

(x > 10) + (y > 5)


What would this expression evaluate to, assuming the same values for x and y)? (The answer requires you to read the section carefully, and then apply what you have learned. I suggest you try that first before trying it at the Python command line.)

11. Both Sections 2.4 and 2.5 mention a type of function that is called a constructor.
1. (5 pts) In general, what do constructors do? (Don’t give an answer that is specific, for instance, to only the constructor for the int data type).

2. (5 pts) Before using the constructor for a Fraction object, a particular line of Python code must be written. What is that line of code? (Be very careful about spelling and upper vs. lower case.)

3. (10 pts) Assuming that line of code has been typed (the one mentioned in the previous question), how do you create a Fraction object that holds the fraction $\frac{4}{5}$ (i.e., “four fifths”), and makes the variable ratio refer to that object?

12. (10 pts) Write the full definition of a function that takes 3 float type of arguments as inputs, x, y, and z, and returns their sum, their average, and the square-root of their sum of squares. When called, for example, as myFunction(2.0, 3.0, 4.0), it should print out the following (it does not have to return anything!):

The sum is: 9.0
The average is: 3.0
The square-root of their sum of squares is: 5.38516480713