Chapter 8 : Structures

example 8.1 page no :77

In [1]:
'''
example 8.1 page no :77
'''

class Point:
    def __init__(self):
        self.x = 0.0
        self.y = 0.0


b = Point()
b.x = 3.0
b.y = 4.0

print b.x , ", " , b.y 
distance = b.x * b.x + b.y * b.y
print "Distance : " , distance
3.0 ,  4.0
Distance :  25.0

example 8.2 page no :79

In [2]:
'''
example 8.2 page no :79
'''

class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y

p1 = Point(3.0, 4.0)
p2 = p1
print p2.x , ", " , p2.y 
3.0 ,  4.0

example 8.3 page no : 80

In [3]:
'''
example 8.3 page no : 80
'''

class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y

def printPoint(p):
    print "(" , p.x , ", " , p.y , ")" 
    
p1 = Point(3.0, 4.0)
printPoint(p1)
( 3.0 ,  4.0 )

example 8.4 page no : 80

In [4]:
'''
example 8.4 page no : 80
'''


class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y

def distance(p1,p2):
    dx = p2.x - p1.x
    dy = p2.y - p1.y
    import math
    return math.sqrt(dx*dx + dy*dy)
    
p1 = Point(3.0, 4.0)
p2 = Point(4.0,5.0)
print distance(p1,p2)
1.41421356237

example 8.5 page no : 81

In [5]:
'''
example 8.5 page no : 81
'''
class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y

def reflect(p):
    temp = p.x
    p.x = p.y
    p.y = temp

def printPoint(p):
    print "(" , p.x , ", " , p.y , ")" 

blank = Point(3.0, 4.0)
printPoint (blank)
reflect (blank)
printPoint (blank)
( 3.0 ,  4.0 )
( 4.0 ,  3.0 )

example 8.6 page no : 84

In [6]:
'''
example 8.6 page no : 84
'''

class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y

class Rectangle:
    def __init__(self,p,x,y):
        self.corner = p
        self.width = x
        self.height = y

c = Point(0.0, 0.0)
b = Rectangle( c, 100.0, 200.0 )
b.width += 50.0
print b.height 
200.0

example 8.7 page no : 84

In [7]:
'''
example 8.7 page no : 84
'''


class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y

class Rectangle:
    def __init__(self,p,x,y):
        self.corner = p
        self.width = x
        self.height = y

def findCenter(box):
    x = box.corner.x + box.width/2
    y = box.corner.y + box.height/2
    result = Point(x, y)
    return result

def printPoint(p):
    print "(" , p.x , ", " , p.y , ")" 

box = Rectangle( Point(0.0, 0.0), 100, 200)
center = findCenter (box);
printPoint (center);
( 50.0 ,  100.0 )

example 8.8 page no : 84

In [8]:
'''
example 8.8 page no : 84
'''

def swap(x,y):
    temp = x[0]
    x[0] = y[0]
    y[0] = temp

i = [7]
j = [9]
swap (i, j)
print i[0] , j[0]
9 7

example 8.9 page no : 85

In [11]:
'''
example 8.9 page no : 85
'''

# prompt the user for input86
print "Enter an integer: ",
# get input
try:
    x = int(raw_input())
    print  x 
#  check and see if the input statement succeeded
except:
    print "That was not an integer." 

# print the value we got from the user

print "What is your name? " , 
name = raw_input()
print name
Enter an integer: a
 That was not an integer.
What is your name? abc
 abc
In [ ]: