In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
In [2]:
for i in range(0,10):
    print(i)
0
1
2
3
4
5
6
7
8
9
In [3]:
students = ["Katie","Bridget","Lexi","Elizabeth"]
In [4]:
for student in students:
    print(student)
Katie
Bridget
Lexi
Elizabeth
In [5]:
for i,student in enumerate(students):
    print(i,student)
0 Katie
1 Bridget
2 Lexi
3 Elizabeth
In [6]:
class Shape:
    
    def __init__(self,name):
        self.name = name
In [7]:
triangle = Shape("triangle")
In [8]:
print(triangle)
<__main__.Shape object at 0x7fe3c791e9e8>
In [9]:
triangle2 = Shape("triangle")
triangle2
Out[9]:
<__main__.Shape at 0x7fe3ce881da0>
In [10]:
class Circle(Shape):
    def __init__(self,r,center,color):
        Shape.__init__(self,"circle")
        self.r = r
        self.diameter = 2. * r
        self.center = center
        self.color = color
        self.cx = self.center[0]
        self.cy = self.center[1]
        
    def __str__(self):
#         return "I am a %s circle" % self.color
        return "I am a circle with center (%d,%d)" % (self.cx,self.cy) 
    
    def getArea(self):
        return (self.r ** 2 * pi)
    
    def getPerimeter(self):
        return (2 * pi * self.r)
In [11]:
circle = Circle(1,(0,0),"r")
print(circle)
I am a circle with center (0,0)
In [12]:
circle.r 
Out[12]:
1
In [13]:
circle.diameter
Out[13]:
2.0
In [14]:
circle.center
Out[14]:
(0, 0)
In [15]:
pi
Out[15]:
3.141592653589793
In [16]:
circle.getPerimeter()
Out[16]:
6.283185307179586
In [17]:
circle1 = Circle(0.5,(0,0),"red")
circle2 = Circle(1,(1,1),"blue")
circle3 = Circle(3,(7,7),"green")
circle4 = Circle(0.25,(3,5),"k")
circle5 = Circle(2,(10,10),"y")
In [18]:
print(circle1)
I am a circle with center (0,0)
In [19]:
circles = [circle1,circle2,circle3,circle4,circle5]
In [20]:
for circle in circles:
    print("The radius of the current circle is %f" % circle.r)
    print("The area of the current circle is %f" % circle.getArea())
The radius of the current circle is 0.500000
The area of the current circle is 0.785398
The radius of the current circle is 1.000000
The area of the current circle is 3.141593
The radius of the current circle is 3.000000
The area of the current circle is 28.274334
The radius of the current circle is 0.250000
The area of the current circle is 0.196350
The radius of the current circle is 2.000000
The area of the current circle is 12.566371
In [21]:
circle_plt = plt.Circle((2.5,2.5),0.5,color="r")
circle_plt_1 = plt.Circle(circle1.center,circle1.r,color=circle1.color)
In [22]:
fig, ax = plt.subplots()
fig.gca().add_artist(circle_plt_1)
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
Out[22]:
(-5, 5)
In [23]: