| 1 |
| h03 |
| CCS CS20 S18 |
| Name: | ||||
|---|---|---|---|---|
| (as it would appear on official course roster) | ||||
| Umail address: | @umail.ucsb.edu | |||
| 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)
| ready? | assigned | due | points |
|---|---|---|---|
| true | Tue 04/17 12:30PM | Sat 04/21 12:00PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
This assignment should be submitted by scanning the pages in the correct order to a PDF file and uploading to gradescope.com.
For more information, visit ucsb-cs8.github.io and look for Gradescope: Student Self Submission under "topics".
Even though it is a Gradescope submission, nevertheless, *please fill in the information at the top of this homework sheet*, including your name and umail address.
READING ASSIGNMENT
Please read Perkovic 2.4-2.5 (Objects, Classes, types, libraries). Then complete these problems.
-
(10 pts) These 10 points are for filling in your name at the top of the sheet, and scanning correctly on Gradescope. (Having these here gives me a place to give you feedback if there is some glitch with the way you are doing it.)
-
As discussed in Section 2.4, the
type()function returns the type of a Python value. When you pass a variable such asx,type(x)returns the type of the value that the variablexcurrently 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]) -
(10 pts) Assume that
casesis the name of a variable with afloatvalue that you want to convert to an integer value. Write a Python expression that convertscasesto an integer (throwing away any fractional part) -
(10 pts) Assume that
courseNumis an integer that represents the numeric part of a course number (e.g. 3, 8, 130, 16, 24). Write a Python expression that convertscourseNumto an string (i.e.<class 'str'>in Python) -
(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
xhas the value20, andyhas the value17, this evaluates toTrue.But what if we accidentally wrote it as:
(x > 10) + (y > 5)What would this expression evaluate to, assuming the same values for
xandy)? (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.) - Both Sections 2.4 and 2.5 mention a type of function that is called a constructor.
-
(5 pts) In general, what do constructors do? (Don’t give an answer that is specific, for instance, to only the constructor for the
intdata type). -
(5 pts) Before using the constructor for a
Fractionobject, 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.) -
(10 pts) Assuming that line of code has been typed (the one mentioned in the previous question), how do you create a
Fractionobject that holds the fraction (i.e., “four fifths”), and makes the variableratiorefer to that object?
-