%pylab inline
class Person():
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "Hi, I am %s, I am %d years old" %(self.name, self.age)
Lexi = Person("Lexi", 23)
print Lexi
class Student(Person):
def __init__(self, name, age, school, major, gpa, year):
Person.__init__(self,name, age)
self.name = name
self.age = age
self.school = school
self.major = major
self.gpa = gpa
self.year = year
def __str__(self):
return "Hi, I am %s, I am a student at %s" % (self.name, self.school)
def yearsToGraduation(self):
return (5 - self.year)
Lexi = Student("Lexi", 23, "Yale", "bioinformatics", 3.5, 3)
print Lexi
print "Lexi has %d years until graduation" % Lexi.yearsToGraduation()
# make a list of students
Mike = Student("Mike", 25, "UConn", "physics", 3.9, 2)
Rachel = Student("Rachel", 28, "Yale", "math", 3.6, 4)
Stephanie = Student("Stephanie", 25, "Southern", "biology", 2.9, 3)
Tim = Student("Tim", 28, "University of New Haven", "physics", 3.5, 4)
Megan = Student("Megan", 22, "Yale", "math", 2.2, 2)
Steven = Student("Steven", 26, "Connecticut College", "biology", 3.4, 4)
students = [Lexi, Mike, Rachel, Stephanie, Tim, Megan, Steven]
for student in students:
print student
print "%s's gpa is %f" % (student.name, student.gpa)
avg_gpa = 0
for student in students:
avg_gpa += student.gpa
print avg_gpa / len(students)
%matplotlib inline
import matplotlib.pyplot as plt
# scatter plot year vs gpa
fig, ax = plt.subplots()
years = []
gpas = []
for student in students:
years.append(student.year)
gpas.append(student.gpa)
ax.scatter(years,gpas)
ax.set_xlabel("Year")
ax.set_ylabel("GPA")