Professional Documents
Culture Documents
27 Ayush
27 Ayush
27 Ayush
Batch: B2
Roll no.: 22
Aim: A. Define a Python class represen ng a basic calculator. Include methods for addi on, subtrac on,
mul plica on, and division.
(A) Code:
print('Roll No.:22')
print('Name:Aniket Raut')
print('Sem: 3rd')
print('Batch: B2')
print("------------------------------------------------------")
class Calculator:
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):
if self.b != 0:
return self.a / self.b
else:
return "infinity"
def sub(self):
return self.a - self.b
(A) Output:
(B) Code:
print('Roll No.:22')
print('Name:Aniket Raut')
print('Sem: 3rd')
print('Batch: B2')
print("------------------------------------------------------")
def fact(no):
if(no == 0):
return 1
elif(no < 0):
return "not possible"
sum = 1
while(no != 1):
sum=sum*no
no = no - 1
return sum
no = int(input("Enter a number:"))
print(f'Factorial of {no} is {fact(no)}')
(B) Output:
© Code:
print('Roll No.:22')
print('Name:Aniket Raut')
print('Sem: 3rd')
print('Batch: B2')
print("------------------------------------------------------")
def isOdd(no):
return no%2
no = int(input("Enter a number:"))
if(isOdd(no)):
print(f'{no} is odd')
else:
print(f'{no} is odd')
© Output:
Conclusion:
In these Python programming tasks, you'll focus on specific concepts such as class defini on, methods, func ons,
condi onal statements, and looping. These concepts are fundamental to building classes and func ons to solve
mathema cal problems or perform logical checks. Python's flexibility and readability make it an excellent choice
for implemen ng such tasks, allowing you to create clear and efficient code for various purposes