1 h03 CS8 F17
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"

# h03: Perkovic 2.4-2.5 (Objects, Classes, types, libraries)

true Tue 10/10 08:00AM Tue 10/17 08:00AM

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.
There is NO MAKEUP for missed assignments, and you may not submit work in advance, or on behalf of another person.
In place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores.)

Please read Perkovic 2.4-2.5 (Objects, Classes, types, libraries). Then complete these problems and turn in your completed homework during your registered lab section..

1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class. It is important to fill in both name and umail every time, since handwriting is sometimes difficult to decipher. Having both helps us ensure you get credit for your work.

Also: while we strongly prefer that you submit your homework on a single sheet of paper, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED.

DO NOT staple, paper clip, spit-fold-and-tear, or do ANYTHING that would make it difficult to automatically feed your paper through a scanner.

2. 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])
3. (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)

4. (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)

5. (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.)

6. 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?