CHAPTER 10: STRUCTURES¶

EXAMPLE ON PAGE:342-343¶

In [1]:

name=[]
price=[]
pages=[]
for i in range(3):
name.append("\0")
price.append(0)
pages.append(0)
print "Enter names,prices and no. of pages of 3 books\n"
for i in range(0,3,1):
name[i]=raw_input()
price[i]=eval(raw_input())
pages[i]=eval(raw_input())
print "\nAnd this is what you entered\n"
for i in range(0,3,1):
print "%c %f %d\n" % (name[i],price[i],pages[i])

Enter names,prices and no. of pages of 3 books

A
100.00
354
C
256.50
682
F
233.70
512

And this is what you entered

A 100.000000 354

C 256.500000 682

F 233.700000 512



EXAMPLE ON PAGE:343-344¶

In [2]:

class book:                                             #YOU CAN ALSO USE CTYPES FOR IMPLEMENTING STRUCTURES
def __init__(self, **kwds):
self.__dict__.update(kwds)
b1=book()
b2=book()
b3=book()
print "Enter names,prices & no. of pages of 3 books\n"
b1.name=raw_input()
b1.price=eval(raw_input())
b1.pages=eval(raw_input())
b2.name=raw_input()
b2.price=eval(raw_input())
b2.pages=eval(raw_input())
b3.name=raw_input()
b3.price=eval(raw_input())
b3.pages=eval(raw_input())
print "And this is what you entered\n"
print "%c %f %d\n" % (b1.name,b1.price,b1.pages)
print "%c %f %d\n" % (b2.name,b2.price,b2.pages)
print "%c %f %d\n" % (b3.name,b3.price,b3.pages)

Enter names,prices & no. of pages of 3 books

A
100.00
354
C
256.50
682
F
233.70
512
And this is what you entered

A 100.000000 354

C 256.500000 682

F 233.700000 512



EXAMPLE ON PAGE:347¶

In [3]:

class book():
def __init__(self,**kwds):
self.__dict__.update(kwds)
b1=book(name='B',price=130.00,pages=550)
print "Address of name=%u\n" % (id(b1.name))
print "Address of price=%u\n" % (id(b1.price))
print "Address of pages=%u\n" % (id(b1.pages))

Address of name=20788864



EXAMPLE ON PAGE:348-349¶

In [4]:

a=0
b=id(a)                             #cause emulator to be linked
class book():
def __init__(self,**kwds):
self.__dict__.update(kwds)
b=[]
for i in range(3):
b.append(0)                         #setting initial value as zero
for i in range(0,3,1):
b[i]=book()
for i in range(0,3,1):
print "Enter name price and pages"
b[i].name=raw_input()
b[i].price=eval(raw_input())
b[i].pages=eval(raw_input())
for i in range(0,3,1):
print "%c %f %d\n" % (b[i].name,b[i].price,b[i].pages)

Enter name price and pages
A
100.00
354
Enter name price and pages
C
256.50
682
Enter name price and pages
F
233.70
512
A 100.000000 354

C 256.500000 682

F 233.700000 512



EXAMPLE ON PAGE:350-351¶

In [5]:

class employee():
def __init__(self,**kwds):
self.__dict__.update(kwds)
e1=employee(name="Sanjay",age=30,salary=5500.50)
e2=employee()
e3=employee()
#piece-meal copying
import copy
e2.name=copy.copy(e1.name)
e2.age=e1.age
e2.salary=e1.salary
#copying all elements at one go
e3=e2
print "%s %d %f\n" % (e1.name,e1.age,e1.salary)
print "%s %d %f\n" % (e2.name,e2.age,e2.salary)
print "%s %d %f\n" % (e3.name,e3.age,e3.salary)

Sanjay 30 5500.500000

Sanjay 30 5500.500000

Sanjay 30 5500.500000



EXAMPLE ON PAGE:351-352¶

In [6]:

def __init__(self, **kwds):
self.__dict__.update(kwds)
class emp():
def __init__(self, **kwds):
self.__dict__.update(kwds)

name=jeru phone=531046

city=nagpur pin=10



EXAMPLE ON PAGE:353¶

In [7]:

class book():
def __init__(self, **kwds):
self.__dict__.update(kwds)
def display(s,t,n):
print "%s %s %d\n" % (s,t,n)
b1=book(name="Let us C",author="YPK",callno=101)
display(b1.name,b1.author,b1.callno)

Let us C YPK 101



EXAMPLE ON PAGE:353-354¶

In [8]:

class book():
def __init__(self, **kwds):
self.__dict__.update(kwds)
def display(b):
print "%s %s %d\n" % (b.name,b.author,b.callno)
b1=book(name="Let us C",author="YPK",callno=101)
display(b1)

Let us C YPK 101



EXAMPLE ON PAGE:354-355¶

In [9]:

class book():
def __init__(self, **kwds):
self.__dict__.update(kwds)
b1=book(name="Let us C",author="YPK",callno=101)
ptr=book()
ptr=b1
print "%s %s %d\n" % (b1.name,b1.author,b1.callno)
print "%s %s %d\n" % (ptr.name,ptr.author,ptr.callno)

Let us C YPK 101

Let us C YPK 101



EXAMPLE ON PAGE:355-356¶

In [10]:

class book():
def __init__(self, **kwds):
self.__dict__.update(kwds)
def display(b):
print "%s %s %d\n" % (b.name,b.author,b.callno)
b1=book(name="Let us C",author="YPK",callno=101)
display(b1)

Let us C YPK 101



EXAMPLE ON PAGE:356¶

In [11]:

class emp():
def __init__(self, **kwds):
self.__dict__.update(kwds)
e=emp(a=0,ch='\0',s=0.0)
print "%u %u %u\n" % (id(e.a),id(e.ch),id(e.s))

20623060 20204312 88374024



EXAMPLE ON PAGE:357-358¶

In [10]:

class emp():
def __init__(self,a,ch,s):                  #Structure without using dictionary
self.a=a
self.ch=ch
self.s=s
e=emp(a=0,ch='\0',s=0.0)
print "%u %u %u\n" % (id(e.a),id(e.ch),id(e.s))

20360916 19942168 88177544