def enum(**enums):
return type('Enum', (), enums)
#Enum declaration
emp_dept = enum(assembly = 0, manufacturing = 1,accounts = 2, stores = 3)
from collections import namedtuple
#Structure declaration
struct_employee = namedtuple("struct_employee", "name age bs emp_dept")
#Structure for employee
department = emp_dept.manufacturing
e = struct_employee("Lothar Mattheus",28,5575.50,department)
#Result
print "Name = ",e.name
print "Age = ",e.age
print "Basic salary = ",e.bs
print "Dept = ",e.emp_dept
if(e.emp_dept == 2):
print "%s is an accountant" %(e.name)
else:
print "%s is not an accountant" %(e.name)
#macro definition
ASSEMBLY = 0
MANUFACTURING = 1
ACCOUNTS = 2
STORES = 3
from collections import namedtuple
#Structure declaration
struct_employee = namedtuple("struct_employee", "name age bs department")
#Structure for employee
e = struct_employee("Lothar Mattheus",28,5575.50,MANUFACTURING)
#Variable declaration
x=6
y=4
#Calculation
a = int( x/y)
#Result
print "Value of a = %f" %(a)
#Variable declaration
x=6
y=4
#Calculation
a = float( x)/y
#Result
print "Value of a = %f" %(a)
#Variable declaration
a=6.35
#Result
print "Value of a on typecasting = %d" %(int(a))
print "Value of a = %f"%(a)
from ctypes import *
import ctypes
import math
#macro definition
MALE = 0
FEMALE = 1
SINGLE = 0
MARRIED = 1
DIVORCED = 2
WIDOWED = 3
#Structure declaration
class employee(Structure):
_fields_ = [("gender", c_int, 1),("mar_stat", c_int, 3),("hobby", c_int, 3),("scheme", c_int, 4)]
_sizeof_ = 2
#Structure for employee
e = employee()
e.gender = MALE
e.mar_stat = DIVORCED
e.hobby = 5
e.scheme =9
#Result
print "Gender = ",e.gender
print "Marital status = ",e.mar_stat
print "Bytes occupied by e = ",sizeof(e)
#Function definition
def display():
print "Long live viruses!!"
#Result
print "Address of function display is ",id(display)
display() #function call
#Function definition
def display():
print "Long live viruses!!"
func_ptr = id(display) #assigning address of function
print "Address of function display is ",func_ptr
display() #function call
#Function definition
def fun():
i = 20
return (i)
#Result
p =fun() #function call
#Function definition
def copy(t,s):
i = 0
while ( s[i] != '\0' ):
t = t + s[i]
i = i + 1
return t
#Variable declaration
source = "Jaded\0"
target = ''
string = copy( target, source ) # function call
#Result
print string
to find out max value from a set of values
#function declaration
def findmax(*arg):
maxi = arg[1]
for count in range(2,arg[0]):
num = arg[count]
if (num > maxi):
maxi = num
return maxi
maxi = findmax(5,23,15,1,92,50)#function call
print "maximum = ",maxi
maxi = findmax(3,100,300,29)#function call
print "maximum = ",maxi
#function declaration
def display(*arg):
if(arg[0] == 1): #case int
for j in range(2,arg[1]+2):
i = arg[j]
print "%d"%(i)
elif(arg[0] == 2): #case char
for j in range(2,arg[1]+2):
i = arg[j]
print "%c"%(i)
elif(arg[0] == 3): #case double
for j in range(2,arg[1]+2):
i = arg[j]
print "%lf"%(i)
#function calls
display(1,2,5,6)
display(2,4,'A','a','b','c')
display(3,3,2.5,299.3,-1.0)
from ctypes import *
#union declaration
class union_a(Union):
_fields_ = [("i", c_int),
("ch", ((c_char * 1)*2))]
key = union_a()
key.i = 512
print "key.i = ",key.i
print "key.ch[0] = ",(key.ch[0][0])
print "key.ch[1] = ",(key.ch[1][0])
from ctypes import *
#Union declaration
class union_a(Union):
_fields_ = [("i", c_int),
("ch", ((c_char * 1)*2))]
key = union_a()
key.i = 512
print "key.i = ",key.i
print "key.ch[0] = ",(key.ch[0][0])
print "key.ch[1] = ",(key.ch[1][0])
key.ch[0][0] = 50 #assign new value to key.ch[0]
print"key.i = ",key.i
print"key.ch[0] = ",(key.ch[0][0])
print"key.ch[1] = ",(key.ch[1][0])
from ctypes import *
#Structure declarations
class a(Structure):
_fields_ = [("i", c_int),
("c", ((c_char * 1)*2))]
class b(Structure):
_fields_ = [("j", c_int),
("d", ((c_char * 1)*2))]
#Union declaration
class union_z(Union):
_fields_ = [("key", a),
("data", b )]
strange = union_z()
strange.key.i = 512
strange.data.d[0][0] = 0
strange.data.d[1][0] = 32
print strange.key.i
print strange.data.j
print strange.key.c[0][0]
print strange.data.d[0][0]
print strange.key.c[1][0]
print strange.data.d[1][0]