Python Pro 51 To 64 Ashish

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

52. Write a program to implement single inheritance.

a. Create the parent class Circle. Initialise the constructor with


the radius of the circle.
b. Defi ne the method get_radius() and calc_area() to know the
radius and area of the
circle.
c. Create the child class named Cylinder. Initialise the value of the
height within the
constructor and call the constructor of the parent class to
initialise the radius of the
cylinder.
d. Finally, defi ne the method Calc_area() in the class Cylinder to
calculate the area of the
cylinder.
Note: Area of Cylinder = 2 * pi * radius * height

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)

x = eval(input("enter the radius = "))


y = eval(input("enter the height = "))
d=Cylinder(x,y)
d.Calc_Area()
d.calc_area()
output:

53. Write a program to implement the concept of multiple inheritance.


a. Create the parent class Shape. Initialise the constructor with Shape.
b. Create another class named Rectangle which inherits the properties of
the parent
class Shape. Defi ne the attributes length and breadth in the Rectangle
class. Initialise
the length and breadth inside the constructor of the Rectangle class. Also
call the
constructor of the parent class to initialise the color of the Rectangle. Defi
ne the method
calc_area() to return the area of the rectangle.
c. Create another class named Triangle which inherits the properties of the
parent class
Shape. Defi ne the attributes base and height in the Triangle class. Initialise
the base
and height inside the constructor of the Triangle class. Also call the
constructor of the
parent class to initialise the color of the Triangle. Defi ne the method
calc_area() to
return the area of the Triangle.
d. Also create the method Tring_Details() in the Triangle class and
Rect_Details() in
the Rectangle Class to return complete details about the rectangle and
triangle.
e. Finally, create the instance of the Rectangle and Triangle classes to
return the area of
the Rectangle and Triangle.

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:

54. create a function sum_all() to accept a variable number of arguments


and display yje sum of all the elements present in it.

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

while index < len(aTup):

rTup += (aTup[index],)

index += 2

return rTup

t=(1, 3, 2, 4, 6, 5)

print(oddTuples(t))

Output:

56. Write a function histogram that takes string as parameter and


generates a frequency of characters contained in it.
S = “AAPPLE”
The program should create a dictionary
D = {‘A’: 2, ‘E’: 1, ‘P’: 2, ‘L’: 1}

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:

from random import randint


fp1 = open("WriteNumRandom.txt","w")
for x in range(51):
x = randint(500,1000)
x = str(x)
fp1.write(x + " ")
fp1.close()

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)

print("sum of salary = ",sum)

main1()

output:

64. Write a function Find_Samllest() which accepts the fi le name as


parameter and reports the
smallest line in the fi le. The content of fi le Demo.txt is

65.WAP to write and read a content from file.

Program:
def Write():

f = open("demo.txt","w")
f.write("Hellow hou are you")

f.write("\n")

f.write("welcome to the chapter file handling")

f.write("\n")

f.write("empty the sample")

Write()

def Read():

f=open("demo.txt","r")

x= (f.read())

print(x)

Read()

Output:

You might also like