Chapter 10: Structures

Arrays , Page number: 365

In [2]:
name = []
price = []
pages = []

n =["A","C","F"]
p=[100.00,256.50,233.70]
pg=[354,682,512]

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

for i in range(0,3):
    #n,p,pg = raw_input("").split()
    print n[i],p[i],pg[i]
    name.append(n[i])
    price.append(p[i])
    pages.append(pg[i])

print  "And this is what you entered" 

for i in range(0,3):
    print name[i], price[i], pages[i] 
Enter names, prices and no. of pages of 3 books 
A 100.0 354
C 256.5 682
F 233.7 512
And this is what you entered
A 100.0 354
C 256.5 682
F 233.7 512

Structure Example , Page number: 366

In [4]:
from collections import namedtuple
#Structure defintion
struct_book = namedtuple("struct_book", "name price pages")

#Input from user
print  "Enter names, prices & no. of pages of 3 books" 
#b1n,b1p,b1pg = raw_input("").split()
#b2n,b2p,b2pg = raw_input("").split()
#b3n,b3p,b3pg = raw_input("").split()
n =["A","C","F"]
p=[100.00,256.50,233.70]
pg=[354,682,512]
print n[0],p[0],pg[0]
print n[1],p[1],pg[1]
print n[2],p[2],pg[2]
#Structures for 3 books
b1 = struct_book(n[0], p[0], pg[0])
b2 = struct_book(n[1], p[1], pg[1])
b3 = struct_book(n[2], p[2], pg[2])


#Result
print "And this is what you entered"  
print  b1.name, b1.price, b1.pages  
print  b2.name, b2.price, b2.pages  
print  b3.name, b3.price, b3.pages 
Enter names, prices & no. of pages of 3 books
A 100.0 354
C 256.5 682
F 233.7 512
And this is what you entered
A 100.0 354
C 256.5 682
F 233.7 512

Memory Map of Structures , Page number: 370

In [5]:
from collections import namedtuple
#Structure defintion
struct_book = namedtuple("struct_book", "name price pages")

#Structures for a book
b1 = struct_book('B', 130.00, 550)

#Result
print  "Address of name = ", id(b1.name )
print  "Address of price = ", id(b1.price )
print  "Address of pages = ", id(b1.pages )
Address of name =  31603728
Address of price =  108997488
Address of pages =  133808864

Array of Structures , Page number: 371

In [6]:
from collections import namedtuple
#Structure defintion
struct_book = namedtuple("struct_book", "name price pages")

#Array of structures
b =[]

n =["A","C","F"]
p=[100.00,256.50,233.70]
pg=[354,682,512]

#Storing data in the array
for i in range(0,3):
    #bn, bp, bpg =raw_input( "Enter name, price and pages: " ).split()
    print "Enter name, price and pages: "
    print n[i],p[i],pg[i]
    b.append(struct_book(n[i], p[i], pg[i]))

#Result
for i in range(0,3):
    print b[i].name, b[i].price, b[i].pages  
Enter name, price and pages: 
A 100.0 354
Enter name, price and pages: 
C 256.5 682
Enter name, price and pages: 
F 233.7 512
A 100.0 354
C 256.5 682
F 233.7 512

Copying Structures , Page number: 374

In [7]:
from collections import namedtuple
#Structure defintion
struct_employee = namedtuple("struct_employee", "name age salary")

#Structures for 3 employees
e1 = struct_employee("Sanjay", 30, 5500.50)
e2 = struct_employee(" ",0,0)
e3 = struct_employee(" ",0,0)

#piece-meal copying 
import copy
e2 = e2._replace(name = e1.name)
e2 = e2._replace(age = e1.age)
e2 = e2._replace(salary = e1.salary)


#copying all elements at one go 
e3 = e2

#Result
print  e1.name, e1.age, e1.salary  
print   e2.name, e2.age, e2.salary  
print   e3.name, e3.age, e3.salary 
Sanjay 30 5500.5
Sanjay 30 5500.5
Sanjay 30 5500.5

Nested Structures , Page number: 375

In [8]:
from collections import namedtuple
#Structure defintions
struct_address = namedtuple("struct_address", "phone city pin")
struct_emp = namedtuple("struct_emp", "name struct_address") #nested structures

#Structures for employee
a = struct_address("531046", "nagpur", 10)
e = struct_emp("jeru",a) #nested structure

#Result
print  "name = %s phone = %s" %( e.name, e.struct_address.phone )
print  "city = %s pin = %d" %( e.struct_address.city, e.struct_address.pin )
name = jeru phone = 531046
city = nagpur pin = 10

Passing Individual Structure Elements to Functions, Page number: 377

In [9]:
from collections import namedtuple
#Structure defintions
struct_book = namedtuple("struct_book", "name author callno")

#Function definition
def display (s,t,n):
    print  s, t, n 
    
#Structures for book
b1 = struct_book("Let us C", "YPK", 101)


display ( b1.name, b1.author, b1.callno ) #function call
Let us C YPK 101

Passing Structure to a Function , Page number: 378

In [10]:
from collections import namedtuple
#Structure defintions
struct_book = namedtuple("struct_book", "name author callno")

#Function definition
def display (b):
    print b.name, b.author, b.callno  
    
#Structures for book
b1 = struct_book("Let us C", "YPK", 101)


display ( b1) #function call
Let us C YPK 101

Structure Pointers , Page number: 379

In [11]:
from collections import namedtuple
#Structure defintions
struct_book = namedtuple("struct_book", "name author callno")

#Structure for book
b1 = struct_book("Let us C", "YPK", 101)
ptr = id(b1) #structure pointer

#Result
print  b1.name, b1.author, b1.callno  
print b1.name, b1.author, b1.callno 
Let us C YPK 101
Let us C YPK 101

Passing Address of a Structure Variable , Page number: 380

In [12]:
#function definition
def display (b):
    print  b.name, b.author, b.callno 
    
from collections import namedtuple
#Structure defintions
struct_book = namedtuple("struct_book", "name author callno")

#Structure for book
b1 = struct_book("Let us C", "YPK", 101)

#function call
display ( b1 ) 
Let us C YPK 101
In [ ]: