Programs of Python Based on Class and Objects
Pythonpedia |
Programs
of Python Based on Class and Objects
1.Program
to Append, Delete and Display Elements of a List Using Classes?
Program:-
Pythonpedia |
Code:-
class check():
def __init__(self):
self.n=[]
def add(self,a):
return self.n.append(a)
def remove(self,b):
self.n.remove(b)
def dis(self):
return (self.n)
obj=check()
choice=1
while choice!=0:
print("0. Exit")
print("1. Add")
print("2. Delete")
print("3. Display")
choice=int(input("Enter choice: "))
if choice==1:
n=int(input("Enter number to append: "))
obj.add(n)
print("List: ",obj.dis())
elif choice==2:
n=int(input("Enter number to remove: "))
obj.remove(n)
print("List: ",obj.dis())
elif choice==3:
print("List: ",obj.dis())
elif choice==0:
print("Exiting!")
else:
print("Invalid choice!!")
print()
Output:-
Pythonpedia |
0. Exit
1. Add
2. Delete
3. Display
Enter choice: 1
Enter number to append: 34
List:
[34]
0. Exit
1. Add
2. Delete
3. Display
Enter choice: 1
Enter number to append: 54
List:
[34, 54]
0. Exit
1. Add
2. Delete
3. Display
Enter choice: 1
Enter number to append: 89
List:
[34, 54, 89]
0. Exit
1. Add
2. Delete
3. Display
Enter choice: 3
List:
[34, 54, 89]
0. Exit
1. Add
2. Delete
3. Display
Enter choice: 0
Exiting!
2.Program
to find area of rectangle?
Program:-
Pythonpedia |
Code:-
class rectangle():
def __init__(self,breadth,length):
self.breadth=breadth
self.length=length
def area(self):
return self.breadth*self.length
a=int(input("Enter length of
rectangle: "))
b=int(input("Enter breadth of
rectangle: "))
obj=rectangle(a,b)
print("Area of
rectangle:",obj.area())
print()
Output:-
Pythonpedia |
Enter length of rectangle: 43
Enter breadth of rectangle: 5
Area of rectangle: 215
3.Program
to find area and perimeter of circle?
Program:-
Pythonpedia |
Code:-
import math
class circle():
def __init__(self,radius):
self.radius=radius
def area(self):
return math.pi*(self.radius**2)
def perimeter(self):
return 2*math.pi*self.radius
r=int(input("Enter radius of
circle: "))
obj=circle(r)
print("Area of
circle:",round(obj.area(),2))
print("Perimeter of
circle:",round(obj.perimeter(),2))
Output:-
Pythonpedia |
Enter radius of circle: 10
Area of circle: 314.16
Perimeter of circle: 62.83
4.Program
to Create a Class which Performs Basic Calculator Operations?
Program:-
Pythonpedia |
Code:-
class cal():
def __init__(self,a,b):
self.a=a
self.b=b
def add(self):
return self.a+self.b
def mul(self):
return self.a*self.b
def div(self):
return self.a/self.b
def sub(self):
return self.a-self.b
a=int(input("Enter first number:
"))
b=int(input("Enter second number:
"))
obj=cal(a,b)
choice=1
while choice!=0:
print("0. Exit")
print("1. Add")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice=int(input("Enter choice: "))
if choice==1:
print("Result: ",obj.add())
elif choice==2:
print("Result: ",obj.sub())
elif choice==3:
print("Result: ",obj.mul())
elif choice==4:
print("Result: ",round(obj.div(),2))
elif choice==0:
print("Exiting!")
else:
print("Invalid choice!!")
print()
Output:-
Pythonpedia |
Enter first number: 40
Enter second number: 30
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 1
Result:
70
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 2
Result:
10
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 0
Exiting!
5
Program to Create
a Class and Get All Possible Subsets from a Set of Distinct Integers?
Program:-
Pythonpedia |
Code:-
class sub:
def f1(self, s1):
return self.f2([], sorted(s1))
def f2(self, curr, s1):
if s1:
return self.f2(curr, s1[1:]) +
self.f2(curr + [s1[0]], s1[1:])
return [curr]
a=[]
n=int(input("Enter number of
elements of list: "))
for i in range(0,n):
b=int(input("Enter element: "))
a.append(b)
print("Subsets: ")
print(sub().f1(a))
Output:-
Pythonpedia |
Enter number of elements of list: 5
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Enter element: 5
Subsets:
[[], [5], [4], [4, 5], [3], [3, 5], [3,
4], [3, 4, 5], [2], [2, 5], [2, 4], [2, 4, 5], [2, 3], [2, 3, 5], [2, 3, 4],
[2, 3, 4, 5], [1], [1, 5], [1, 4], [1, 4, 5], [1, 3], [1, 3, 5], [1, 3, 4], [1,
3, 4, 5], [1, 2], [1, 2, 5], [1, 2, 4], [1, 2, 4, 5], [1, 2, 3], [1, 2, 3, 5],
[1, 2, 3, 4], [1, 2, 3, 4, 5]]
-Thank
you
Comments
Post a Comment