s = "9.30am:10.30pm"
s.split(":")
['9.30am', '10.30pm']
result = []
result.append("Tony")
print(result)
['Tony']
d = {"A": 4, "B":3, "C":2, "D":1, "F":0}
list(d.items())
[('A', 4), ('B', 3), ('C', 2), ('D', 1), ('F', 0)]
s1 = [1,2,3]
s2 = [1,2,3]
s1.append(4)
print(s1,s2)
[1, 2, 3, 4] [1, 2, 3]
s1 = [1,2,3]
s2 = s1
s1.append(4)
print(s1,s2)
[1, 2, 3, 4] [1, 2, 3, 4]
class Student:
# constructor method
def __init__(self,n,m):
self.name = n
self.major = m
self.grades = []
# getter method
def get_name(self):
return self.name
# getter method
def get_major(self):
return self.major
# setter method
def set_major(self,m):
self.major = m
# other method (can be called a modifier)
def add_grade(self,c,t,g):
self.grades.append((c,t,g))
def gpa(self):
num_grade = {"A": 4, "B":3, "C":2, "D":1, "F":0}
points = 0
credits = 0
for c in self.grades:
credits = credits + c[1]
points = points + c[1] * num_grade[c[2]]
if credits > 0:
return round(points/credits,2)
else:
return None
s5 = Student("John","CSC")
print(s5.name,s5.major,s5.grades)
print(s5.get_name())
John CSC [] John
s2 = Student("Alice","Math")
print(s2.name,s2.major,s2.grades)
print(s2.get_name())
Alice Math [] Alice
s2.set_major("CSC")
print(s2.major)
CSC
s5.add_grade("1301",4,"A")
s5.add_grade("1302",4,"A")
s5.add_grade("2510",3,"C")
print(s5.grades)
print(s2.grades)
[('1301', 4, 'A'), ('1302', 4, 'A'), ('2510', 3, 'C')] []
print(s5.gpa())
print(s2.gpa())
3.45 None
38/11
3.4545454545454546
class Rational:
def __init__(self,n,d):
self.numerator = n
self.denominator = d
self.reduce()