class Counter:
def __init__(self): #constructor
self.__count = 0
def get_count(self): #return count
return self.__count
def __iadd__(self,other): #increment
self.__count += other
return self
c1 = Counter() #define and initialize
c2 = Counter()
print 'c1 =',c1.get_count() #display
print 'c2 =',c2.get_count()
c1 += 1 #increment c1
c2 += 1 #increment c2
c2 += 1 #increment c3
print 'c1 =',c1.get_count() #display again
print 'c2 =',c2.get_count()
class Counter:
def __init__(self): #constructor
self.count = 0
def get_count(self): #return count
return self.count
def __iadd__(self,other): #increment count
self.count += other #increment count
temp = Counter() #make a temporary Counter
temp.count = self.count #give it same value as this obj
return temp #return the copy
c1 = Counter() #c1=0, c2=0
c2 = Counter()
print 'c1 =',c1.get_count() #display
print 'c2 =',c2.get_count()
c1 += 1 #c1=1
c1 += 1 #c1=2, c2=2
c2 = c1
print 'c1 =',c1.get_count() #display again
print 'c2 =',c2.get_count()
class Counter:
def __init__(self,c=0): #overloaded constructor
self.__count = c
def get_count(self): #return count
return self.__count
def __iadd__(self,other): #increment count
self.__count += other #increment count, then return
return Counter(self.__count) #an unnamed temporary object initialized to this count
c1 = Counter() #c1=0, c2=0
c2 = Counter()
print 'c1 =',c1.get_count() #display
print 'c2 =',c2.get_count()
c1 += 1 #c1=1
c1 += 1 #c1=2, c2=2
c2 = c1
print 'c1 =',c1.get_count() #display again
print 'c2 =',c2.get_count()
from copy import deepcopy
class Counter:
def __init__(self,c=0): #constructor
self.__count = c
def get_count(self): #return count
return self.__count
def __iadd__(self,other): #increment count (prefix & postifix both) ; 'In python no ++ opertor is there'
self.__count += other
return Counter(self.__count)
c1 = Counter() #c1=0, c2=0
c2 = Counter()
print 'c1 =',c1.get_count() #display
print 'c2 =',c2.get_count()
c1 += 1 #c1=1
c1 += 1 #c1=2, c2=2
c2 = c1
print 'c1 =',c1.get_count() #display
print 'c2 =',c2.get_count()
c2 = deepcopy(c1)
c1 += 1 #c1=3, c2=3
print 'c1 =',c1.get_count() #display again
print 'c2 =',c2.get_count()
class Distance: #class Distance
def __init__(self,ft=0,inc=0.0): #overloaded constructor
self.__feet = ft
self.__inches = inc
def getdist(self): #get length from user
self.__feet = input("Enter feet: ")
self.__inches = input("Enter inches: ")
def showdist(self): #display distance
print self.__feet , '\' -' , self.__inches , '\"'
def __add__(self,d2): #add 2 distances
f = self.__feet + d2.__feet #add the feet
i = self.__inches + d2.__inches #add the inches
if i >= 12.0: #if total exceeds 12.0
i -= 12.0 #decrease inches by 12.0 and
f += 1 #increase feet by 1
return Distance(f,i) #return a temporary Distance initialized to sum
dist1 = Distance() #define distances
dist3 = Distance()
dist4 = Distance()
dist1.getdist() #get dist1 from user
dist2 = Distance(11,6.25) #define , initialize dist2
dist3 = dist1 + dist2 #single '+' operator
dist4 = dist1 + dist2 + dist3 #multiple '+' operator
print '\ndist1 =',; dist1.showdist() #display all length
print 'dist2 =',; dist2.showdist()
print 'dist3 =',; dist3.showdist()
print 'dist4 =',; dist4.showdist()
class String: #user - defined string type
__SZ = 80 #size of String objects
def __init__(self,s=""): #overloaded constructor
self.__str = s
def display(self): #display string
print self.__str ,
def __add__(self,ss): #add strings
temp = String() #make temporary string
if len(self.__str) + len(ss.__str) < self.__SZ:
temp.__str = self.__str + " " + ss.__str #add this string and arg string to temp string
else:
print '\nString overflow'
raise SystemExit
return temp #return temp string
s1 = String("\nMerry Christmus! ") #intialize the string variables
s2 = String("Happy new year!")
s3 = String()
s1.display() #display strings
s2.display()
s3.display()
s3 = s1 + s2 #add s2 to s1 and assign to s3
s3.display() #display s3
print
class Distance: #class Distance
def __init__(self,ft=0,inc=0.0): #overloaded constructor
self.__feet = ft
self.__inches = inc
def getdist(self): #get length from user
self.__feet = input("Enter feet: ")
self.__inches = input("Enter inches: ")
def showdist(self): #display distance
print self.__feet , '\' -' , self.__inches , '\"'
def __lt__(self,d2): #compare this distance with d2
bf1 = self.__feet + self.__inches/12
bf2 = d2.__feet + d2.__inches/12
return bf1 < bf2
dist1 = Distance() #define Distance dist1
dist1.getdist() #get dist1 from user
dist2 = Distance(11,6.25) #define and initialize dist2
print '\ndist1 =',; dist1.showdist() #display Distances
print 'dist2 =',; dist2.showdist()
if dist1 < dist2: #overloaded '<' operator
print 'dist1 is less than dist2'
else:
print 'dist2 is greater than (or equal to) dist2'
class String: #user-define String type
__SZ = 80 #size of string objects
def __init__(self,s=""): #constructor
self.__str = s
def display(self): #display a string
print self.__str ,
def getstr(self): #read a string
self.__str = raw_input()
def __eq__(self,ss): #check for equality
return self.__str == ss.__str
s1 = String("yes")
s2 = String("no")
s3 = String()
print "\n Enter 'yes' or 'no': ",
s3.getstr() #get string from user
if s3 == s1: #compare from "yes"
print 'You typed yes\n'
elif s3 == s2: #compare from "no"
print 'you typed no\n'
else:
print "you didn't follow instructions\n"
class Distance: #class Distance
def __init__(self,ft=0,inc=0.0): #constructor
self.__feet = ft
self.__inches = inc
def getdist(self): #get length from user
self.__feet = input("Enter feet: ")
self.__inches = input("Enter inches: ")
def showdist(self): #display Distance
print self.__feet , '\' -' , self.__inches , '\"'
def __iadd__(self,d2): #add distance to this one
self.__feet = self.__feet + d2.__feet #add the feet
self.__inches = self.__inches + d2.__inches #add the inches
if self.__inches >= 12.0: #if total exceeds 12.0,
self.__inches -= 12.0 #then decrease inches by 12.0 and
self.__feet += 1 #increase feet by 1
return self
dist1 = Distance() #define dist1
dist1.getdist() #get dist1 from user
print '\ndist1 =',; dist1.showdist()
dist2 = Distance(11,6.25) #define, initialize dist2
print 'dist2 =',; dist2.showdist()
dist1 += dist2 #dist1 = dist1 + dist2
print 'After addition,'
print 'dist1 =',; dist1.showdist()
LIMIT = 100
class safearay:
__arr = [0 for j in range(LIMIT)]
def putel(self,n,elvalue): #set value of element
if n<0 or n>=LIMIT:
print 'Index out of bound'
self.__arr[n] = elvalue
def getel(self,n): #get value of element
if n<0 or n>=LIMIT:
print 'Index out of bound'
return self.__arr[n]
sa1 = safearay()
for j in range(LIMIT): #insert element
sa1.putel(j,j*10)
for j in range(LIMIT): #display element
temp = sa1.getel(j)
print 'Element',j,'is',temp
LIMIT = 100
class safearay:
__arr = [0 for j in range(LIMIT)]
def access1(self,n,n1): #function for set the value in the array
if n<0 or n>=LIMIT:
print 'Index out of bound'
self.__arr[n] = n1
def access2(self,n): #function for return the array
if n<0 or n>=LIMIT:
print 'Index out of bound'
return self.__arr[n]
sa1 = safearay()
for j in range(LIMIT): #insert elements using first function
sa1.access1(j,j*10)
for j in range(LIMIT): #display elements using second function
temp = sa1.access2(j)
print 'Element',j,'is',temp
LIMIT = 100
class safearay:
__arr = [0 for j in range(LIMIT)]
def op1(self,n,n1): #function for set the value in the array
if n<0 or n>=LIMIT:
print 'Index out of bound'
self.__arr[n] = n1
def op2(self,n): #function for return the array
if n<0 or n>=LIMIT:
print 'Index out of bound'
return self.__arr[n]
sa1 = safearay()
for j in range(LIMIT): #insert elements using first function
sa1.op1(j,j*10)
for j in range(LIMIT): #display elements using second function
temp = sa1.op2(j)
print 'Element',j,'is',temp
class Distance: #class Distance
def __init__(self,meters=None,ft=0,inc=0.0): #constructor
if isinstance(meters,float): #one argument
self.__MTF = 3.280833 #convert meters to Distance,
fltfeet = self.__MTF*meters #convert to float feet
self.__feet = int(fltfeet) #feet is integer part
self.__inches = 12*(fltfeet-self.__feet) #inches is what's left
else: #three arguments (first should be None)
self.__MTF = 3.280833
self.__feet = ft
self.__inches = inc
def getdist(self): #get length from user
self.__feet = input("Enter feet: ")
self.__inches = input("Enter inches: ")
def showdist(self): #display Distance
print self.__feet , '\' -' , self.__inches , '\"'
def co(self): #conversion function, converts Distance to meters
fracfeet = self.__inches/12 #convert the inches
fracfeet += float(self.__feet) #add the feet
return fracfeet/self.__MTF #convert to meters
dist1 = Distance(2.35) #uses 1-arg constructor to convert meters to Distance
print 'dist1 =',; dist1.showdist()
mtrs = dist1.co() #uses conversion function for Distance to meters
print 'dist1 =',mtrs,'meters'
dist2 = Distance(None,5,10.25) #uses 2-arg constructor
mtrs = dist2.co() #also uses conversion function
print 'dist2 =',mtrs,'meters'
class String: #user-defined string type
__SZ = 80 #size of all String type
def __init__(self,s = None): #constructor with zero or one argument
self.__str = s
def display(self): #display the String
print self.__str,
def char(self): #conversion function
return self.__str
xstr = "Joyeux Noel! "
s1 = String(xstr) #use 1-arg constructor
s1.display() #display string
s2 = String("Bonne Annee!") #use 1-arg constructor to initialize string
print s2.char() #uses conversion fuction
class time12:
def __init__(self,ap=True,h=0,m=0): #constructor
self.__pm = ap #true = pm, false = am
self.__hrs = h # 1 to 12
self.__mins = m # 0 to 59
def display(self): #format: 11:59 p.m.
print self.__hrs,':',
if self.__mins < 10:
print '0', #extra zero for "01"
print self.__mins,
if self.__pm:
print 'p.m.'
else:
print 'a.m.'
class time24:
def __init__(self,h=0,m=0,s=0): #constructor
self.__hours = h # 0 to 23
self.__minutes = m # 0 to 59
self.__seconds = s # 0 to 59
def display(self): #format: 23:15:01
if self.__hours < 10:
print '0',
print self.__hours,':',
if self.__minutes < 10:
print '0',
print self.__minutes,':',
if self.__seconds < 10:
print '0',
print self.__seconds
def time_12(self): #conversion from 24-hour time to 12-hour time
hrs24 = self.__hours
if self.__hours < 12: #find am/pm
pm = False
else:
pm = True
if self.__seconds < 30: #round secs
roundMins = self.__minutes
else:
roundMins = self.__minutes + 1
if roundMins == 60: #carry mins?
roundMins = 0
hrs24 += 1
if hrs24 == 12 or hrs24 == 24: #carry hrs?
if pm == True: #toggle am/pm
pm = False
else:
pm = True
if hrs24 < 13:
hrs12 = hrs24
else:
hrs12 = hrs24-12
if hrs12 == 0: # 00 is 12 a.m.
hrs12 = 12
pm = False
return time12(pm,hrs12,roundMins)
while True:
print '\nEnter 24-hour time: '
h = input(" Hours (0 to 23): ") #get 24-hr time from user
if h > 23: #quit if hours > 23
break
m = input(" Minutes: ")
s = input(" Seconds: ")
t24 = time24(h,m,s) #make a time24
print 'You entered:',
t24.display() #display the time24
t12 = t24.time_12() #convert time24 to time12
print '12-hour time:', #display equivalent time12
t12.display()
class time24:
def __init__(self,h=0,m=0,s=0): #constructor
self.__hours = h # 0 to 23
self.__minutes = m # 0 to 59
self.__seconds = s # 0 to 59
def display(self): #format: 23:15:01
if self.__hours < 10:
print '0',
print self.__hours,':',
if self.__minutes < 10:
print '0',
print self.__minutes,':',
if self.__seconds < 10:
print '0',
print self.__seconds
def getHrs(self):
return self.__hours
def getMins(self):
return self.__minutes
def getSecs(self):
return self.__seconds
class time12:
def __init__(self,ap=True,h=0,m=0): #constructor
if isinstance(ap,bool):
self.__pm = ap #true = pm, false = am
self.__hrs = h # 1 to 12
self.__mins = m # 0 to 59
else: #conversion from 24-hour time to 12-hour time
t24 = ap
hrs24 = t24.getHrs()
if t24.getHrs() < 12: #find am/pm
self.__pm = False
else:
self.__pm = True
if t24.getSecs() < 30: #round secs
self.__mins = t24.getMins()
else:
self.__mins = t24.getMins() + 1
if self.__mins == 60: #carry mins?
self.__mins = 0
hrs24 += 1
if hrs24 == 12 or hrs24 == 24: #carry hrs?
if self.__pm == True: #toggle am/pm
self.__pm = False
else:
self.__pm = True
if hrs24 < 13:
self.__hrs = hrs24
else:
self.__hrs = hrs24-12
if self.__hrs == 0: # 00 is 12 a.m.
self.__hrs = 12
self.__pm = False
def display(self): #format: 11:59 p.m.
print self.__hrs,':',
if self.__mins < 10:
print '0', #extra zero for "01"
print self.__mins,
if self.__pm:
print 'p.m.'
else:
print 'a.m.'
while True:
print '\nEnter 24-hour time: '
h = input(" Hours (0 to 23): ") #get 24-hr time from user
if h > 23: #quit if hours > 23
break
m = input(" Minutes: ")
s = input(" Seconds: ")
t24 = time24(h,m,s) #make a time24
print 'You entered:',
t24.display() #display the time24
t12 = time12(t24) #convert time24 to time12
print '12-hour time:', #display equivalent time12
t12.display()
class Distance: #class Distance
def __init__(self,meters=None,ft=0,inc=0.0): #constructor
if isinstance(meters,float): #one argument
self.__MTF = 3.280833 #convert meters to Distance,
fltfeet = self.__MTF*meters #convert to float feet
self.__feet = int(fltfeet) #feet is integer part
self.__inches = 12*(fltfeet-self.__feet) #inches is what's left
else: #three arguments (first should be None)
self.__MTF = 3.280833
self.__feet = ft
self.__inches = inc
def showdist(self): #display Distance
print self.__feet , '\' -' , self.__inches , '\"'
def fancyDist(d):
print '(in feet and inches) =',
d.showdist()
dist1 = Distance(2.35)
print 'dist1 =',; dist1.showdist()
mtrs = 3.0
print 'dist1'
class scrollbar:
def __init__(self,sz,own): #constructor
self.__size = sz
self.__owner = own
def setSize(self,sz): #change size
self.__size = sz
def setOwner(self,own): #change owner
self.__owner = own
def getSize(self): #returns size
return self.__size
def getOwner(self): #returns owner
return self.__owner
sbar = scrollbar(60,"Window1")
sbar.setOwner("Window2")
print sbar.getSize(),', ',sbar.getOwner()