Are there any questions before I begin?
I have posted a solution to homework 8 here.
After I finish talking I will pass out the papers for today's graded quiz.
Write your name clearly at the top.
When you finish the Quiz hand it to me.
Then you can work on the Class Exercise and today's ungraded quiz.
class CLASS_NAME(Object):
def __init__(self, id, last_name, first_name):
self._id = id
self._last_name = last_name
self._first_name = first_name
self._preferred_name = ""
self._phonetic_name = ""
self._email = ""
self._unix_username = ""
def get_id(self):
return self._id
def get_last_name(self):
return self._last_name
def get_first_name(self):
return self._first_name
def get_preferred_name(self):
return self._preferred_name
def get_phonetic_name(self):
return self._phonetic_name
def get_email(self):
return self._email
def get_unix_username(self):
return self._unix_username
def set_preferred_name(self, preferred_name):
self._preferred_name = preferred_name
def set_phonetic_name(self, phonetic_name):
self._phonetic_name = phonetic_name
def set_email(self, email):
self._email = email
def set_unix_username(self, unix_username):
self._unix_username = unix_username
def __str__(self):
return self._first_name + " " + self._last_name + ", " + self._id
def get_full_name(self):
if self._preferred_name:
return self._preferred_name + " " + self._last_name
else:
return self._first_name + " " + self._last_name
def get_sort_name(self):
if self._preferred_name:
return self._last_name + ", " + self._preferred_name
else:
return self._last_name + ", " + self._first_name
import pickle
pickle_file = open(pickle_filename, "wb")
pickle.dump(students, pickle_file)
pickle_file = open(pickle_filename, "rb")
students = pickle.load(pickle_file)
import statements at the top of the file ...from student import Student import pickle class Section: ...
x = 5
x += 7 y = x + 8 x > y
diff = t1.difference(t2)
diff = t1 - t2
| Operator | Magic Method |
|---|---|
| + | __add__(self, other) |
| - | __sub__(self, other) |
| * | __mul__(self, other) |
| // | __floordiv__(self, other) |
| / | __truediv__(self, other) |
| % | __mod__(self, other) |
| ** | __pow__(self, other) |
# returns the difference in seconds between two times
def __sub__(self, other_time):
return self._seconds - other_time._seconds
>>> t1 = Time("10:45:10")
>>> t2 = Time("10:50:00")
>>> t2 - t1
290
>>> t2 - t1
| Operator | Magic Method |
|---|---|
| == | __eq__(self, other) |
| != | __ne__(self, other) |
| < | __lt__(self, other) |
| > | __gt__(self, other) |
| <= | __le__(self, other) |
| >= | __ge__(self, other) |
def __eq__(self, other):
return self._seconds == other._seconds
def __ne__(self, other):
return self._seconds != other._seconds
def __lt__(self, other):
return self._seconds < other._seconds
def __gt__(self, other):
return self._seconds > other._seconds
def __le__(self, other):
return self._seconds <= other._seconds
def __ge__(self,other):
return self._seconds >= other._seconds
>>> t1 = Time("10:45:10")
>>> t2 = Time("10:50:00")
>>> t3 = Time("10:50:00")
>>> t1 == t2
False
t2 == t3
True
>>> t1 < t2
True
>>> t1 > t2
False
>>> t1 <= t2
True
>>> t2 <= t3
True
>>> t1 >= t2
False
>>> t2 >= t3
True
intfloatboolstr| Operator | Magic Method |
|---|---|
int |
__int__(self) |
float |
__float__(self) |
bool |
__bool__(self) |
str |
__str__(self) |
def __int__(self):
return self.__seconds
def __float__(self):
return self.__seconds * 1.0
True
>>> bool(0) False >>> bool(5) True >>> bool(-5) True
def __bool__(self):
return self.__seconds != 0
>>> t1 = Time("10:15:00")
>>> t2 = Time("00:00:00")
>>> str(t1)
'10:15:0 AM'
>>> bool(t1)
True
>>> bool(t2)
False
>>> int(t1)
36900
>>> int(t2)
0
>>> float(t1)
36900.0
def __eq__(self, other):
return self._seconds == other._seconds
def __eq__(self, other):
return self._manufacturer == other.get_manufacturer() and self._model == other.get_model()
def __ne__(self, other):
return self._manufacturer != other.get_manufacturer() or self._model != other.get_model()
c1 = Car("Honda", "CRV", "1997", "Blue")
c2 = Car("Honda", "CRV", "2010", "Green")
c3 = Car("Honda", "Civic", "2015", "Red")
print(c1)
print(c2)
print(c3)
print("c1 == c2 :" , c1 == c2 )
print("c1 == c3 :" , c1 == c3 )
print("c1 != c3 :" , c1 != c3 )
Honda CRV 1997 Blue Honda CRV 2010 Green Honda Civic 2015 Red c1 == c2 : True c1 == c3 : False c1 != c3 : True
>>> from car4 import Car Honda CRV 1997 Blue Honda CRV 2010 Green Honda Civic 2015 Red c1 == c2 : True c1 == c3 : False c1 != c3 : True
if statement
if __name__ == "__main__":
c1 = Car("Honda", "CRV", "1997", "Blue")
c2 = Car("Honda", "CRV", "2010", "Green")
c3 = Car("Honda", "Civic", "2015", "Red")
print(c1)
print(c2)
print(c3)
print("c1 == c2 :" , c1 == c2 )
print("c1 == c3 :" , c1 == c3 )
print("c1 != c3 :" , c1 != c3 )
$ ./car3.py Honda CRV 1997 Blue Honda CRV 2010 Green Honda Civic 2015 Red c1 == c2 : True c1 == c3 : False c1 != c3 : True
>>> from car3 import Car >>>
ii
def __init__(self, real, imaginary): self._real = int(real) self._imag = int(imaginary)
5 + 3iwhere
i is the square root of minus 1
def __str__(self): return str(self._real) + " + " + str(self._imag) + "i"
def __add__(self, other): return Complex(self._real + other._real, self._imag + other._imag)
def __sub__(self, other): return Complex(self._real - other._real, self._imag - other._imag)
def __mul__(self, other): new_real = self._real * other._real - self._imag * other._imag new_imag = self._real * other._imag + self._imag * other._real return Complex(new_real, new_imag)
if __name__ == "__main__":
c1 = Complex(3,2)
c2 = Complex(1,7)
print("c1:", c1)
print("c2:", c2)
print("c1 + c2:", c1 + c2)
print("c1 - c2:", c1 - c2)
print("c1 * c2:", c1 * c2)
$ python3 complex.py c1: 3 + 2i c2: 1 + 7i c1 + c2: 4 + 9i c1 - c2: 2 + -5i c1 * c2: -11 + 23i