Python Pro 51 To 64 Ashish
Python Pro 51 To 64 Ashish
Python Pro 51 To 64 Ashish
Program:
class circle:
radius =0
def __init__(self,radius):
self.radius = radius
def get_radius(self):
return self.radius
def calc_area(self):
pi=3.14
AreaOfCircle = pi*self.radius**2
print("area of circle = ", AreaOfCircle)
class Cylinder(circle):
height =0
def __init__(self,radius,height):
circle.__init__(self,radius)
self.height = height
def Calc_Area(self):
pi=3.14
areaofcylinder = 2* pi * self.radius * self.height
print("AreaOfCylinder = ", areaofcylinder)
Program:
class shape:
color =""
def __init__(self):
self.color = "red"
class Rectangle(shape):
length =0
breadth =0
def __init__(self,length,breadth):
self.length = length
self.breadth = breadth
shape.__init__(self)
def Calc_area(self):
AraaOfRectangle = self.length*self.breadth
return AraaOfRectangle
def Rec_Detail(self,rect):
return rect
class Triangle(shape):
base =0
height =0
def __init__(self,base,height):
self.base = base
self.height=height
def Calc_area(self):
var =1/2
AreaOftriangle =var*(self.base*self.height)
return str(AreaOftriangle)
def Tring_Detail(self,areaoftringle):
return areaoftringle
d=Rectangle(12,17)
c=d.Calc_area()
AreaofRectangle = d.Rec_Detail(c)
print("AreaofRectangle = ",AreaofRectangle)
m =Triangle(45.2,23.5)
l= m.Calc_area()
AreaOfTriangle = m.Tring_Detail(l)
print("AreaOfTriangle = ",AreaOfTriangle)
Output:
Program:
def sum_all(*args):
t=()
s=0
for i in args:
s=s+i
print(s)
sum_all(10,40,50,60,70)
sum_all(24,50,60,60)
Output:
55.Consider a tuple t=(1,3,2,4,6,5). Write a program to store numbers
present at odd index into a new tuple.
Program:
def oddTuples(aTup):
rTup = ()
index = 0
rTup += (aTup[index],)
index += 2
return rTup
t=(1, 3, 2, 4, 6, 5)
print(oddTuples(t))
Output:
Program:
def Histogram(S):
D =dict()
for C in S:
if C not in D:
D[C] = 1
else:
D[C]=D[C]+1
return D
H=Histogram("AAPPLE")
print(H)
Output:
57. Write a program to pass a list to a function. Calculate the total number
of positive and negative numbers from the list and then display the count in
terms of dictionary.
Input: L=[1,-2,-3,4]
Output: {‘Neg’: 2, ‘Pos’: 2}
Program:
def abc(L):
D={}
D["Pos"]=0
D["Neg"]=0
for x in L:
if x>0:
D["Pos"]+=1
else:
D["Neg"]+=1
print(D)
L=[1,-2,-3,4,5,-6,7-8]
abc(L)
output:
58. Write a function which takes a tuple as a parameter and returns a new
tuple as the output, where every other element of the input tuple is copied,
starting from the first one.
T = (‘Hello’,’Are’,’You’,’Loving’,’Python?’)
Output_Tuple = (‘Hello’, ‘You’, ‘Python?’)
Program:
def get_tup(tup):
t=()
t=tup[0:len(tup):2]
return t
T = ('Hello','Are','You','Loving','Python?')
print("\t T = ",T)
Output_Tuple = get_tup(T)
print("Output_Tuple = ",Output_Tuple)
Output:
59. Write a function called how_many, which returns the sum of the
number of values associated with a dictionary.
T= animals = {‘L’:[‘Lion’],’D’:[‘Donkey’],’E’:[‘Elephant’]}
>>>print(how_many(animals))
Program:
def how_many(dict1):
return len(dict1.keys())
animals = {'L':['Lion'],'D':['Donkey'],'E':['Elephant']}
print("Animals : ",animals)
print("how_many(animals) : ",how_many(animals))
Output:
60. Write a function ‘biggest’ which takes a dictionary as a parameter and
returns the key corresponding to the entry with the largest number of
values associated with it.
>>>animals = {‘L’:[‘Lion’],’D’:[‘Donkey’,’Deer’],’E’:[‘Elepha
nt’]}
>>>biggest(animals)
>>>d #Since d contains two values
Program:
def beggest(animals):
for key,value in animals.items():
if value==max(animals.values()):
return value
animals= {'L':['Lion'],'D':['Donkey','Deer'],'E':['Elephant']}
print("beggest = ",beggest(animals))
Output:
61. Write a function Count_Each_vowel which accepts string from a user. The function
should
return a dictionary which contains the count of each vowel.
>>> Count_Each_vowel(“HELLO”)
>>>{‘H’:1, ‘E’:1, ‘L’:2 , ‘O’:2}
62. Generate 50 random numbers within a range 500 to 1000 and write
them to file Write NumRandom.txt.
Program:
output:
63.Write a program to add the contents of a fi le salary.txt and display the
sum of salaries of all
employees present in the file. The content of fi le salary.txt is
Program:
def main():
x=4
f=open("salary.txt","w")
f.write("10000")
f.write("\n")
f.write("2000")
f.write("\n")
f.write("1200")
f.write("\n")
f.write("134000")
f.write("\n")
f.write("21000")
main()
def main1():
f=open("salary.txt","r")
sum =0
print("content of file ")
while True:
x= f.readline()
print(x)
if not x:
break
sum =sum+int(x)
main1()
output:
Program:
def Write():
f = open("demo.txt","w")
f.write("Hellow hou are you")
f.write("\n")
f.write("\n")
Write()
def Read():
f=open("demo.txt","r")
x= (f.read())
print(x)
Read()
Output: