Q.1 Write a python program to accept and convert string in uppercase or vice versa.
=input("Enter String")
str1= ""
newStr
for i in range(0, len(str1)):
if str1[i].islower():
+= str1[i].upper()
newStr elif str1[i].isupper():
+= str1[i].lower()
newStr else:
+= str1[i];
newStr print("String after case conversion : " + newStr)
OUTPUT
Enter StringRaHUl SonaWane
String after case conversion : rAhuL sONAwANE
Q2 Write a python program to create a class Calculator with basic calculator operations (addition, subtraction, division, multiplication, remainder).
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
def rem(self):
return self.a%self.b
=int(input("Enter first number: "))
a=int(input("Enter second number: "))
b=cal(a,b)
obj=1
choicewhile choice!=0:
print("0. Exit")
print("1. Add")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Reminder")
=int(input("Enter choice: "))
choiceif 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==5:
print("Result: ",obj.rem())
elif choice==0:
print("Exiting!")
else:
print("Invalid choice!!")
print()
OUTPUT
Enter first number: 8
Enter second number: 4
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
5. Reminder
Enter choice: 1
Result: 12
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
5. Reminder
Enter choice: 2
Result: 4
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
5. Reminder
Enter choice: 3
Result: 32
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
5. Reminder
Enter choice: 4
Result: 2.0
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
5. Reminder
Enter choice: 5
Result: 0
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
5. Reminder
Enter choice: 0
Exiting!
Q2. Write a python program to perform operations on sets which includes union of two sets, an intersection of sets, set difference and a symmetric difference.
= {0, 2, 4, 6, 8};
E = {1, 2, 3, 4, 5};
N
print("Union of E and N is",E | N)
print("Intersection of E and N is",E & N)
print("Difference of E and N is",E - N)
print("Symmetric difference of E and N is",E ^ N)
OUTPUT
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {0, 8, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}
0 Comments:
Post a Comment