#Display size of structure elements
import sys
#Class is used instead of structure in python
class book1:
book = ''
pages = 0
price = 0.00
#Python variables uses value tagged methods for storing the values
#Class variable declaration
bk1 = book1()
#Result
sys.stdout.write("\nSize of Structure Elements")
sys.stdout.write("\nBook : %d"%(sys.getsizeof(bk1.book)))
sys.stdout.write("\nPages : %d"%(sys.getsizeof(bk1.pages)))
sys.stdout.write("\nPrice : %d"%(sys.getsizeof(bk1.price)))
sys.stdout.write("\nTotal Bytes : %d"%(sys.getsizeof(bk1)))
#Define a structure and initialize its member variables
import sys
#Class declaration
class book1:
book = ''
pages = 0
price = 0.0
#Class variable declaration and Initialization
bk1 = book1()
bk1.book = "C++"
bk1.pages = 300
bk1.price = 285
#Result
sys.stdout.write("\nBook Name : %s"%(bk1.book))
sys.stdout.write("\nNo. of Pages : %d"%(bk1.pages))
sys.stdout.write("\nBook Price : %d"%(bk1.price))
#Copy structure elements from one object to another object
import sys
#Class declaration
class disk:
co = ''
type1 = 0.0
price = 0
#Class variable declaration
d1 = disk()
d2 = disk()
d3 = disk()
#Class variable initialization
d1.co = "SONY"
d1.type1 = 1.44
d1.price = 20
#Copying
d2.co = d1.co
d2.type1 = d1.type1
d2.price = d1.price
d3 = d2
#Result
sys.stdout.write("\n%s %g %d"%(d1.co,d1.type1,d1.price))
sys.stdout.write("\n%s %g %d"%(d2.co,d2.type1,d2.price))
sys.stdout.write("\n%s %g %d"%(d3.co,d3.type1,d3.price))
#Read values and assign them to structure variables
import sys
#Class declaration
class book1:
book = ''
pages = 0
price = 0.0
#Class variable declaration
bk1 = book1()
#Variable initialization
bk1.book = raw_input("Enter Book name,pages,price : ")
bk1.pages = int(raw_input("Enter Book name,pages,price : "))
bk1.price = float(raw_input("Enter Book name, pages,price : "))
#Result
sys.stdout.write("\nBook Name : %s"%(bk1.book))
sys.stdout.write("\nNo. of Pages : %d"%(bk1.pages))
sys.stdout.write("\nBook Price : %d"%(bk1.price))
#Read and display car details
import sys
#Class declaration
class time:
second = 0
minute = 0
hour = 0
class t:
carno = 0
st = time()
rt = time()
#Class variable declaration
r1 = t()
#Variable Initialization
r1.carno = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.st.hour = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.st.minute = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.st.second = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.rt.hour = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.rt.minute = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.rt.second = int(raw_input("Car No., Starting Time, Reaching Time :"))
#Result
sys.stdout.write("\nCar No. \tStarting Time \tReaching Time\n")
sys.stdout.write("%d\t"%(r1.carno))
sys.stdout.write("\t%d:%d:%d\t\t"%(r1.st.hour,r1.st.minute,r1.st.second))
sys.stdout.write("%d:%d:%d"%(r1.rt.hour,r1.rt.minute,r1.rt.second))
#Read and display the details of a person
import sys
#Class declaration
class name:
first = ''
second = ''
last = ''
class b_date:
day = 0
month = 0
year = 0
class data:
nm = name()
bt = b_date()
#Class variable declaration
r1 = data()
#Variable Initialization
r1.nm.first = raw_input("Enter Name (First/Second/Last)")
r1.nm.second = raw_input("Enter Name (First/Second/Last)")
r1.nm.last = raw_input("Enter Name (First/Second/Last)")
r1.bt.day = int(raw_input("Enter Birth Date Day/Month/Year"))
r1.bt.month = int(raw_input("Enter Birth Date Day/Month/Year"))
r1.bt.year = int(raw_input("Enter Birth Date Day/Month/Year"))
#Result
sys.stdout.write("Name : %s %s %s\n"%(r1.nm.first,r1.nm.second,r1.nm.last))
sys.stdout.write("Birth Date : %d.%d.%d"%(r1.bt.day,r1.bt.month,r1.bt.year))
#Create array of structure objects
import sys
#Class declaration
class time:
second = 0
minute = 0
hour = 0
class t:
carno = 0
st = time()
rt = time()
#Class variable declaration
r1 = [t() for i in range(0,3)]
#Variable Initialization
for k in range(0,3):
r1[k].carno = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
r1[k].st.hour = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
r1[k].st.minute = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
r1[k].st.second = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
r1[k].rt.hour = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
r1[k].rt.minute = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
r1[k].rt.second = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
#Result
sys.stdout.write("\nCar No.\t\tStarting Time\tReaching Time")
for k in range(0,3):
sys.stdout.write("\n%d\t"%(r1[k].carno))
sys.stdout.write("\t%d:%d:%d\t"%(r1[k].st.hour,r1[k].st.minute,r1[k].st.second))
sys.stdout.write("\t%d:%d:%d\t"%(r1[k].rt.hour,r1[k].rt.minute,r1[k].rt.second))
#Read and display student details
import sys
#Class declaration
class stud:
name = ''
rollno = 0
grade = ''
#Class variable declaration
st = [stud() for i in range(0,3)]
#Variable Initialization
k = 0
while k < 3:
st[k].name = raw_input("Name :")
st[k].rollno = int(raw_input("Roll No. :"))
st[k].grade = raw_input("Grade : ")
k += 1
#Result
k = 0
sys.stdout.write("\nName \tRollno \tGrade\n")
while k < 3:
sys.stdout.write("\n%s\t%d\t%s"%(st[k].name,st[k].rollno,st[k].grade))
k += 1
#Pointer to structure
import sys
#Class declaration
class book:
name = ''
author = ''
pages = 0
#Class variable declaration
b1 = book()
#Variable Initialization
b1.name = "JAVA COMPLETE REFERENCE"
b1.author = "P.NAUGHTON"
b1.pages = 886
#There is no pointer concept in python
#Result
sys.stdout.write("\n%s by %s of %d pages"%(b1.name,b1.author,b1.pages))
sys.stdout.write("\n%s by %s of %d pages"%(b1.name,b1.author,b1.pages))
#Pointer as members of structure
import sys
#Class declaration
class boy:
name = ''
age = 0
height = 0.0
#Class variable declaration
sp = boy()
#Variable Initialization
sp.name = "Mahesh"
sp.age = 20
sp.height = 5.40
#There is no pointer concept in python
#Result
sys.stdout.write("\nName = %s"%(sp.name))
sys.stdout.write("\nAge = %s"%(sp.age))
sys.stdout.write("\nHeight = %s"%(sp.height))
#Pointer as members of structure
import sys
#Class declaration
class boy:
name = ''
age = 0
height = 0.0
#Class variable declaration
b = boy()
#Variable Initialization
nm = "Somesh"
ag = 20
ht = 5.40
#There is no pointer concept in python
b.name = nm
b.age = ag
b.height = ht
#Result
sys.stdout.write("\nName : %s"%(b.name))
sys.stdout.write("\nAge = %d"%(b.age))
sys.stdout.write("\nHeight = %.2g"%(b.height))
#Display the contents of the structure using ordinary pointer
import sys
#Class declaration
class num:
a = 0
b = 0
c = 0
#Class variable declaration
d = num()
#Variable Initialization
d.a = 2
d.b = 3
d.c = 4
#There is no pointer concept in python
#Result
sys.stdout.write("\na = %d"%(d.a))
sys.stdout.write("\nb = %d"%(d.b))
sys.stdout.write("\nc = %d"%(d.c))
#Passing address of structure variable
import sys
#Class declaration
class book:
name = ''
author = ''
pages = 0
#Class variable declaration and initialization
b1 = book()
b1.name = "JAVA COMPLETE REFERENCE"
b1.author = "P.NAUGHTON"
b1.pages = 886
#Function definition
def show(b1):
sys.stdout.write("\n%s by %s of %d pages"%(b1.name,b1.author,b1.pages))
#Function call
show(b1)
#Passing structure elements to function
import sys
#Class declaration
class boy:
name = ''
age = 0
wt = 0
#Class variable declaration and initialization
b1 = boy()
b1.name = "Amit"
b1.age = 20
b1.wt = 25
#Function definition
def print1(s,t,n):
sys.stdout.write("\n%s %d %d"%(s,t,n))
#Function call
print1(b1.name,b1.age,b1.wt)
#Pass entire structure to user defined function
import sys
#Class declaration
class boy:
name = ''
age = 0
wt = 0
#Class variable declaration and initialization
b1 = boy()
b1.name = "Amit"
b1.age = 20
b1.wt = 25
#Function definition
def print1(b):
sys.stdout.write("\n%s %d %d"%(b.name,b.age,b1.wt))
#Function call
print1(b1)
#User defined data type
import sys
#There is no preprocessor directive in python
H = 60
#There is no typedef function in python and no separate variable declaration is needed
hrs = int(raw_input("Enter Hours :"))
#Result
sys.stdout.write("\nMinutes = %d"%(hrs*H))
sys.stdout.write("\nSeconds = %d"%(hrs*H*H))
#String data type
import sys
#There is no typedef function in python
a = " Hello "
b = raw_input("Enter Your Name : ")
#Result
sys.stdout.write("%s %s"%(a,b))
#Create user defined data type from structure
import sys
#Class declaration
#There is no typedef function in python
class struct:
name = ''
sex = ''
acno = 0
#class variable declaration and initialization
employee = struct()
employee.name = "Sanjay"
employee.sex = "M"
employee.acno = 125
#Result
sys.stdout.write("\nName\tSex\tA/c No.\n")
sys.stdout.write("%s\t"%(employee.name))
sys.stdout.write("%s\t"%(employee.sex))
sys.stdout.write("%s\n"%(employee.acno))
#Create user defined data type from structure
import sys
#Class declaration
class struct:
name = ''
sex = ''
acno = 0
#class Variable declaration and initialization
employee = [struct() for i in range(0,2)]
for k in range(0,2):
employee[k].name = raw_input("Name of the Employee :")
employee[k].sex = raw_input("Sex")
employee[k].acno = raw_input("A/c No.")
#Result
sys.stdout.write("\nName\tSex\tA/c No.\n")
for k in range(0,2):
sys.stdout.write("%s\t"%(employee[k].name))
sys.stdout.write("%s\t"%(employee[k].sex))
sys.stdout.write("%s\n"%(employee[k].acno))
#Structure containing the details of the employees
import sys
#There is no typedef function in python
#Class definition
class struct:
first = ''
middle = ''
last = ''
city = ''
pincode = 0
#Class variable declaration and initialization
person = [struct() for i in range(0,2)]
for j in range(0,2):
person[j].first = raw_input("First Name :")
person[j].middle = raw_input("Middle Name : ")
person[j].last = raw_input("Last Name : ")
person[j].city = raw_input("City & Pincode")
person[j].pincode = int(raw_input("City & Pincode"))
#Result
for j in range(0,2):
sys.stdout.write("\nRecord No : %d"%(j+1))
sys.stdout.write("\nFirst Name : %s"%(person[j].first))
sys.stdout.write("\nMiddle Name : %s"%(person[j].middle))
sys.stdout.write("\nLast Name : %s"%(person[j].last))
sys.stdout.write("\nCity & Pincode : %s - %d\n"%(person[j].city,person[j].pincode))
#Information of vehicles
import sys
#Variable Initialization
PETROL = 1
DISEL = 2
TWO_WH = 3
FOUR_WH = 4
OLD = 5
NEW = 6
#Class declaration
class vehicle:
type1 = 3
fuel = 2
model = 3
#Class variable declaration and initialization
v = vehicle()
v.type1 = FOUR_WH
v.fuel = DISEL
v.model = OLD
#Result
sys.stdout.write("\nType of Vehicle : %d"%(v.type1))
sys.stdout.write("\nFuel : %d"%(v.fuel))
sys.stdout.write("\nModel : %d"%(v.model))
#Display the examination result of the student
import sys
#Variable Initialization
#There is no preprocessor directives in python
PASS = 1
FAIL = 0
A = 0
B = 1
C = 2
#Class declaration
class student:
name = ''
result = 1
grade = 2
#Class variable declaration and initialization
v = student()
v.name = "Sachin"
v.result = PASS
v.grade = C
#Result
sys.stdout.write("\nName : %s"%(v.name))
sys.stdout.write("\nResult : %d"%(v.result))
sys.stdout.write("\nGrade : %d"%(v.grade))
#Enumerated data type for 12 months
import sys
#There is no enumerated data type in python and an alternate is to use class
#Class declaration
class month:
Jan = 0
Feb = 1
Mar = 2
Apr = 3
May = 4
June = 5
July = 6
Aug = 7
Sep = 8
Oct = 9
Nov = 10
Dec = 11
#Class variable declaration
c = month()
#Result
sys.stdout.write("\nJan = %d"%(c.Jan))
sys.stdout.write("\nFeb = %d"%(c.Feb))
sys.stdout.write("\nJune = %d"%(c.June))
sys.stdout.write("\nDec = %d"%(c.Dec))
#Enumerated data type
import sys
#There is no enumerated data type in python and an alternate is to use class
#Class declaration
class month:
Jan = 1
Feb = 2
Mar = 3
Apr = 4
May = 5
June = 6
July = 7
Aug = 8
Sep = 9
Oct = 10
Nov = 11
Dec = 12
#Class variable declaration
c = month()
#Result
sys.stdout.write("\nJan = %d"%(c.Jan))
sys.stdout.write("\nFeb = %d"%(c.Feb))
sys.stdout.write("\nJune = %d"%(c.June))
sys.stdout.write("\nDec = %d"%(c.Dec))
#Display name of month using enumerated data type
import sys
#There is no enumerated data type in python and an alternate is to use class
#Class declaration
class month:
Jan = 1
Feb = 2
Mar = 3
Apr = 4
May = 5
June = 6
July = 7
Aug = 8
Sep = 9
Oct = 10
Nov = 11
Dec = 12
#Class variable declaration
c = month()
#Result
for f in range(c.Jan,c.Dec+1):
#There is no switch case statement in python
if f == c.Jan:
sys.stdout.write("\nJanuary")
else:
if f == c.Feb:
sys.stdout.write("\nFebruary")
else:
if f == c.Mar:
sys.stdout.write("\nMarch")
else:
if f == c.Apr:
sys.stdout.write("\nApril")
else:
if f == c.May:
sys.stdout.write("\nMay")
else:
if f == c.June:
sys.stdout.write("\nJune")
else:
if f == c.July:
sys.stdout.write("\nJuly")
else:
if f == c.Aug:
sys.stdout.write("\nAugust")
else:
if f == c.Sep:
sys.stdout.write("\nSeptember")
else:
if f == c.Oct:
sys.stdout.write("\nOctober")
else:
if f == c.Nov:
sys.stdout.write("\nNovember")
else:
sys.stdout.write("\nDecember")
#Enumerated data type
import sys
#There is no enumerated data type in python. class is used instead
class capital:
Mumbai = 0
Hyderabad = 1
Bangalore = 2
class state:
name = ''
c = capital()
#Class variable declaration
s = state()
c1 = capital()
#Class variable initialization
s.name = "Andhra Pradesh"
s.c = s.c.Hyderabad
#Result
sys.stdout.write("\nState : %s"%(s.name))
sys.stdout.write("\nCapital : %d"%(s.c))
if s.c == c1.Hyderabad:
sys.stdout.write("\nHyderabad is Capital of %s"%(s.name))
#Identify the type of entered character using enumerated data type.
import sys
#Class declaration instead of enumerated data type
class ctype:
Letter = 0
Digit = 1
Other = 2
#Variable Initialization
ch = raw_input("Enter any character")
c = ctype()
f = ch.isalpha()
#Result
if f != 0:
sys.stdout.write("\n%c is %d type of symbol"%(ch,c.Letter))
else:
f = ch.isdigit()
if f != 0:
sys.stdout.write("\n%c is %d type of symbol"%(ch,c.Digit))
else:
sys.stdout.write("\n%c is %d type of symbol"%(ch,c.Other))
#Size of union and number of bytes reserved for it
import sys
#There is no union/structure in python. class is used instead
#Class declaration
class result:
marks = 0
grade = ''
class res:
name = ''
age = 0
perf = result()
#Class variable declaration
data = res()
#Result
sys.stdout.write("Size of Union : %d\n"%(sys.getsizeof(data.perf)))
sys.stdout.write("Size of Structure : %d\n"%(sys.getsizeof(data)))
#in python, value tagged method is used for data storage and can represent large numbers
#Memory size of the computer
import psutil
psutil.phymem_usage()
#Displays current status of the memory
#different systems will have different memory status
#Display the system time at specified cursor position
import sys
import datetime
#Result
sys.stdout.write("%s"%(datetime.datetime.now()))
#Change the cursor in different sizes
import turtle
#Ipython supports graphics through turtle module
from Tkinter import *
import Tkinter
top = Tkinter.Tk()
B1 = Tkinter.Button(top, text ="circle", relief=RAISED,\
cursor="circle")
B2 = Tkinter.Button(top, text ="plus", relief=RAISED,\
cursor="plus")
B1.pack()
B2.pack()
#top.mainloop()
#Create a directory using dos interrupt
import os
import sys
#Variable Initialization
dir1 = raw_input("Enter Directory Name : ")
#Result
if not os.path.exists(dir1):
os.makedirs(dir1)
sys.stdout.write("Directory %s created"%(dir1))
else:
sys.stdout.write("Directory %s not created"%(dir1))
#Display the given character on the screen
import sys
#Variable Initialization
dl = 67
#Result
sys.stdout.write("%c"%(chr(dl)))
#Display the attributes of a file using DOS interrupt
import sys
import os
print os.stat("IO.SYS")
#Delete a file using dos interrupt
import os
import sys
#Variable Initialization
file1 = raw_input("Enter a file name : ")
try:
os.remove(file1)
sys.stdout.write("File successfully deleted")
except:
sys.stdout.write("File could not be deleted")
#Structuret within union
import sys
#there is no union/structure in python. class is used instead
#Class declaration
class x:
f = 0.0
p = ['' for i in range(0,2)]
class z:
set1 = x()
#Class variable declaration and initialization
st = z()
st.set1.f = 5.5
st.set1.p[0] = 65
st.set1.p[1] = 66
#Result
sys.stdout.write("\n%g"%(st.set1.f))
sys.stdout.write("\n%c"%(st.set1.p[0]))
sys.stdout.write("\n%c"%(st.set1.p[1]))