Python Slip 20

24dee43934ae46b985665625ead4fd55

Q.1 Write a python program to accept and convert string in uppercase or vice versa.

str1=input("Enter String")  
newStr = ""  
   
for i in range(0, len(str1)):  
    if str1[i].islower():   
        newStr += str1[i].upper()
    elif str1[i].isupper():  
        newStr += str1[i].lower()
    else:  
        newStr += str1[i];          
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
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")
    print("5. Reminder")
    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==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.

E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};

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