In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
In [2]:
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)
    
        
In [3]:
Lexi = Person("Lexi", 23)
In [4]:
print Lexi
Hi, I am Lexi, I am 23 years old
In [5]:
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)
    
    
In [6]:
Lexi = Student("Lexi", 23, "Yale", "bioinformatics", 3.5, 3)
In [7]:
print Lexi
print "Lexi has %d years until graduation" % Lexi.yearsToGraduation()
Hi, I am Lexi, I am a student at Yale
Lexi has 2 years until graduation
In [8]:
# 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)
In [9]:
students = [Lexi, Mike, Rachel, Stephanie, Tim, Megan, Steven]
In [10]:
for student in students:
    print student
    print "%s's gpa is %f" % (student.name, student.gpa)
Hi, I am Lexi, I am a student at Yale
Lexi's gpa is 3.500000
Hi, I am Mike, I am a student at UConn
Mike's gpa is 3.900000
Hi, I am Rachel, I am a student at Yale
Rachel's gpa is 3.600000
Hi, I am Stephanie, I am a student at Southern
Stephanie's gpa is 2.900000
Hi, I am Tim, I am a student at University of New Haven
Tim's gpa is 3.500000
Hi, I am Megan, I am a student at Yale
Megan's gpa is 2.200000
Hi, I am Steven, I am a student at Connecticut College
Steven's gpa is 3.400000
In [11]:
avg_gpa = 0
for student in students:
    avg_gpa += student.gpa
print avg_gpa / len(students)
3.28571428571
In [12]:
%matplotlib inline
import matplotlib.pyplot as plt
In [13]:
# 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")
Out[13]:
<matplotlib.text.Text at 0x7f2ad21318d0>
In [14]: