def emp_dept(*sequential, **named): #definition of enumeration
enums = dict(zip(sequential, range(len(sequential))), **named)
return type('Enum', (), enums)
class employee(): #structure
def __init__(self,**kwds):
self.__dict__.update(kwds)
department=emp_dept('assembly','manufacturing','accounts','stores') #defining a variable of type enum emp_dept
e=employee(name="Lothar Mattheus",age=28,bs=5575.50,department=department.manufacturing)
print "Name=%s\n" % (e.name)
print "Age=%d\n" % (e.age)
print "Basic salary=%f\n" % (e.bs)
print "Dept=%d\n" % (e.department)
if e.department==department.accounts:
print "%s is an accountant\n" % (e.name)
else:
print "%s is not an accountant\n" % (e.name)
Name=Lothar Mattheus Age=28 Basic salary=5575.500000 Dept=1 Lothar Mattheus is not an accountant
def ASSEMBLY():
return 0
def MANUFACTURING():
return 1
def ACCOUNTS():
return 2
def STORES():
return 3
class employee():
def __init__(self,**kwds):
self.__dict__.update(kwds)
e=employee()
e.name="Lothar Mattheus"
e.age=28
e.bs=5575.50
e.department=MANUFACTURING()
print "Name=%s\n" % (e.name)
print "Age=%d\n" % (e.age)
print "Basic salary=%f\n" % (e.bs)
print "Dept=%d\n" % (e.department)
Name=Lothar Mattheus Age=28 Basic salary=5575.500000 Dept=1
x=6
y=4
a=x/y #int
print "Value of a=%f\n" % (a)
Value of a=1.000000
x=6
y=4
a=float(x)/y #float
print "Value of a=%f\n" % (a)
Value of a=1.500000
a=6.35
print "Value of a on type casting=%d\n" % (int(a)) #converts float to int
print "Value of a=%f\n" % (a)
Value of a on type casting=6 Value of a=6.350000
from ctypes import *
def MALE():
return 0
def FEMALE():
return 1
def SINGLE():
return 0
def MARRIED():
return 1
def DIVORCED():
return 2
def WIDOWED():
return 3
class employee(Structure):
_fields_= [("gender",c_short, 1), #1 bit size for storage
("mar_stat", c_short, 2), #2 bit size for storage
("hobby",c_short, 3), #3 bit size for storage
("scheme",c_short, 4)] #4 bit size for storage
e=employee()
e.gender=MALE()
e.mar_status=DIVORCED()
e.hobby=5
e.scheme=9
print "Gender=%d\n" % (e.gender)
print "Marital status=%d\n" % (e.mar_status)
import ctypes
print "Bytes occupied by e=%d\n" % (ctypes.sizeof(e))
Gender=0 Marital status=2 Bytes occupied by e=2
def display():
print "Long live viruses!!\n"
print "Address of function display is %s\n" % (display)
display()
Address of function display is <function display at 0x02D93DF0> Long live viruses!!
def display():
print "\nLong live viruses!!"
func_ptr=display
print "Address of function display is %s\n" % (display)
func_ptr()
Address of function display is <function display at 0x05696730> Long live viruses!!
def fun():
i=20
return (id(i))
p=fun
p()
20688356
def copy(t,s):
t="\0"
for item in s:
t=t+item
return t
source="Jaded"
target=[]
str1=copy(target,source)
print "%s\n" % (str1)