Chapter 12 : Union

Example: 1, Page No.5.87

In [1]:
#program using union.

class union_name:
    a=0
    b=[0,1]
    
c=union_name()
c.a=256

print "c.a value is %d\n "%c.a
print "c.b[0] value is %d\n"%c.b[0]
print "c.b[1] value is %d\n"%c.b[1]
c.a value is 256
 
c.b[0] value is 0

c.b[1] value is 1

Example: 2, Page No.:5.87

In [5]:
#program to find size of union and number of bytes

import sys

class union_result:
    marks=0
    grade=''
    
    
class struct_res:
    name=[15]
    age=0
    sex=''
    address=''
    pincode=0
    perf=union_result()
    
data=struct_res()   

#Result
print "Size of Union: %d\n"%sys.getsizeof(data.perf)
print "Size of Structure: %d\n"%sys.getsizeof(data)
Size of Union: 32

Size of Structure: 32

In [ ]:
#Pointer to Unions cannot convert as no pointer in python. Page no. 5.88

Example: 3, Page No.:5.89

In [6]:
#Program that includes of initial values to structure variable

import sys


class union_id:
    color='20'
    size=0
    

class struct_clothes:
    manufacturer='30'
    cost=0.0
    description=union_id()
    
    
shirt=struct_clothes()
shirt.manufacturer='Indian'
shirt.cost=35.00
shirt.description.color='Black'

print "%d\n"%(sys.getsizeof(union_id))

print "%s %f "%(shirt.manufacturer,shirt.cost),"%s %d\n "%(shirt.description.color, shirt.description.size)
 

shirt.description.size =12


print "%s %f "%(shirt.manufacturer, shirt.cost),"%s %d\n "%(shirt.description.color, shirt.description.size)
48

Indian 35.000000  Black 0
 
Indian 35.000000  Black 12
 

Example: 4, Page no.: 5.92

In [7]:
#Program to read and write employee and their date of joining using nested structure. 


class date:
    day=0
    month=""
    year=0
    
class employee:
    code=0
    name=''
    salary=0.0
    doj=date()
    
    
emp1=employee()

emp1.code=input("Enter Employee Code: ")
emp1.name=raw_input("Enter the Employee Name: ")
emp1.salary=input("Enter the Employee Salary: ")

print "\nEnter Date of joining in order\n"
emp1.doj.day=input("Enter day: ")
emp1.doj.month=raw_input("Enter month: ")
emp1.doj.year=input("Enter year: ")

print "\nThe Employee Code is: %d"%emp1.code
print "\nThe Employee Name is: %s"%emp1.name
print "\nThe Employee Name is: %f"%emp1.salary
print "\nThe Employee DOJ is: %d %s %d"%(emp1.doj.day,emp1.doj.month,emp1.doj.year)
Enter Employee Code: 200
Enter the Employee Name: VIJI
Enter the Employee Salary: 2000.00

Enter Date of joining in order

Enter day: 12
Enter month: December
Enter year: 2004

The Employee Code is: 200

The Employee Name is: VIJI

The Employee Name is: 2000.000000

The Employee DOJ is: 12 December 2004

Example: 5, Page No. 5.93

In [8]:
#Program to store 3 records in one structure


class book:
    name=''
    price=0
    pages=0
    
    
b=[book() for a in range(0,3)]

for i in range(0,3):
    b[i].name=raw_input("Enter the Book Name: ")
    b[i].price=input("Enter the Book Price: ")
    b[i].pages=input("Enter the Book Pages: ")
    
print '\n'    
for i in range(0,3):
    print "%s\t"%(b[i].name),"%d\t"%(b[i].price),"%d\n"%(b[i].pages)
Enter the Book Name: English
Enter the Book Price: 165
Enter the Book Pages: 200
Enter the Book Name: Maths
Enter the Book Price: 300
Enter the Book Pages: 450
Enter the Book Name: Physics
Enter the Book Price: 250
Enter the Book Pages: 370


English	165	200

Maths	300	450

Physics	250	370

Example: 6, Page No. 5.95

In [9]:
#Program to copy of entire structure to a function

class std:
    no=0
    avg=0.0

a=std()

a.no=15
a.avg=90.75

def func(Struct_p):
    print "Number is.... %d\n"%a.no
    print "Average is... %f"%a.avg
    
func(a)

    
Number is.... 15

Average is... 90.750000

Example: 7, Page No.:5.97

In [6]:
#Programs shows how the structure can be accessed by a pointer variable.

class student:
    roll_no=0
    name=''
    marks=0.0
stud1=student()

print "Enter the student details: \n"
stud1.roll_no=input()
stud1.name=raw_input()
stud1.marks=input()

print "\nDisplay of structure using structure variable\n"
print "Roll No\t Name\t Marks"
print "\n-------\t ----- -------\n"
print "%d\t %s\t %f"%(stud1.roll_no,stud1.name,stud1.marks)

pt=stud1

print "\nDisplay of structure using pointer variable\n"
print "Roll No\t Name\t Marks"
print "\n-------\t ----- -------\n"
print "%d\t %s\t %f"%(pt.roll_no,pt.name,pt.marks)
Enter the student details: 

39
Muni
77

Display of structure using structure variable

Roll No	 Name	 Marks

-------	 ----- -------

39	 Muni	 77.000000

Display of structure using pointer variable

Roll No	 Name	 Marks

-------	 ----- -------

39	 Muni	 77.000000

Case Study: 1, Page No.:5.98

In [24]:
#Program to transfer a structure to a function by passing the structure address to the function

class struct:
    name=''
    no=0
    age=0
record=struct()
std=record
std.name,std.no,std.age=["LAK",15,25]
print "%s\t%d\t%d"%(std.name,std.no,std.age)

def fun(std):
    pt.name="Muni"
    pt.no=16
    pt.age=26
    
std.name=pt.name
std.no=pt.no
std.age=pt.age

print "%s\t%d\t%d"%(std.name,std.no,std.age)
    
    
LAK	15	25
Muni	16	26

Case Study: 2, Page No.:5.100

In [5]:
#Program to pass an array of structure to a function, and return a pointer to a structure.
import sys
N=3
NULL=0

class struct:
    sno=0
    name=''
    age=0
    
record=struct()
std=record

print "Student Number Locater"
print "To Exit the program enter '0' for student number"
stdno=input("Enter the Student Number: ")
print "\n"
if stdno ==15:

    std.sno,std.name,std.age=[15,"MUNI",28]
    print "Number .... %d \n"%(std.sno)
    print "Name ...... %s\n"%(std.name)
    print "Age ....... %d\n"%(std.age)
if stdno == 16:
    std.sno,std.name,std.age=[16,"LAK",27]
    print "Number .... %d \n"%(std.sno)
    print "Name ...... %s\n"%(std.name)
    print "Age ....... %d\n"%(std.age)
    
if stdno == 17:
    std.sno,std.name,std.age=[17,"RAJA",31]
    print "Number .... %d \n"%(std.sno)
    print "Name ...... %s\n"%(std.name)
    print "Age ....... %d\n"%(std.age)
    
if stdno==0:
    exit
Student Number Locater
To Exit the program enter '0' for student number
Enter the Student Number: 15


Number .... 15 

Name ...... MUNI

Age ....... 28

Case Study: 3, Page No.: 5.101

In [10]:
#Program to print student name and mark using structure

class student:
    name=''
    marks=0.00
    
s1=''
f=0.00

student1=student()

student2=student()


student1.name=raw_input("Enter Name: ")
f=input("Enter Mark: ")

student2.marks=f

print "\nName is %s \n"%student1.name
print "Marks are %f \n"%student2.marks
Enter Name: Venkat
Enter Mark: 89

Name is Venkat 

Marks are 89.000000 

Case Study 4, Page No.: 5.102

In [11]:
#Program to print marks, grade and their address using union



class union_marks:
    perc=0.00
    grade=''
    
student1=union_marks()

student1.perc=98.5

print "Marks are %f \t address is %8lu\n"%(student1.perc, id(student1.perc))

student1.grade='A'

print "Grade is %c \t address is %8lu\n"%(student1.grade,id(student1.grade))
Marks are 98.500000 	 address is 158368916

Grade is A 	 address is 3073485064