27 Ayush

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

PYTHON PRACTICAL 2

Name: Aniket Raut

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.

B. Write a Python func on to find the factorial of a given number.

C. Create a Python program that checks if a given number is even or odd.

(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

cal = Calculator(int(input("Enter 1st number:")) , int(input("Enter 2nd number")))


print(f'Addition:{cal.add()}')
print(f'Substraction:{cal.sub()}')
print(f'Mutiplication:{cal.mul()}')
print(f'Division:{cal.div()}')

(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

You might also like