Python Slip 19

f7c1c0837b53467ea0a7d488dfa59922

Q.1 Write a python program to get a single string from two given strings and swap the first two characters of each string.

Sample String: 'abc', 'xyz' Expected Output: xycabz

a=input("Enter First String:-\t")

b=input("Enter Second String:-\t")

x=a[0:2]

a=a.replace(a[0:2],b[0:2])

b=b.replace(b[0:2],x)

print(a,b)

OUTPUT

Enter First String:-	rahul
Enter Second String:-	sagar
sahul ragar

Q2 Define a class Person having members – name, address. Create a subclass called ―Employee with members staff id, salary. Create ‘n’ objects of the Employee class and display all the details of the Employee.

class person:
    def __init__(self,name,address):
        self.empname=name
        self.address=address
    
class employee(person):
    def __init__(self, name, address,salary):
        super().__init__(name, address)
        self.salary=salary
    def display(self):
        print('name : {}\taddress : {}\tsalary : {}'.format(self.empname,self.address,self.salary))   
   
n=int(input("Enter How may Employees:"))
s=[]
for i in range(0,n):
    x=input("Enter Object Name:")
    s.append(x)
    print(s)
for j in range(0,n):
    print("\nEnter Details of Employee",j+1)
    name1=input('enter name : ')
    address=input('enter address : ')
    salary=int(input('enter salary : '))
    s[j]=employee(name1,address,salary)

print("\nDetails of Employees Are:-")
for j in range(0,n):
    s[j].display()

Q2. Write a python program to create a tuple of n numbers and print maximum, minimum, and sum of elements in a tuple. (Don’t use built-in functions)

import math
tup = tuple()
n=int(input("Enter the Number of Elements in the Tuple\t"))
for i in range(1, n+1):
    value = int(input("Enter the %d Set value = " %i))
    tup=tup+(value,)
print("tuple Items = ", tup)
length=0
maximum= 0
minimum= math.inf #Set the initial min to infinity
sumtup=0
for i in tup:
    length+= 1
    sumtup+= i
    if maximum < i:
        maximum = i
    if minimum > i:
        minimum = i
print("Tuple Length = ", length)
print("Maximum in Tuple = ", maximum)
print("Minimum in Tuple = ", minimum)
print("Sum of Tuple Elements = ", sumtup)

OUTPUT

Enter the Number of Elements in the Tuple	5
Enter the 1 Set value = 6
Enter the 2 Set value = 5
Enter the 3 Set value = 4
Enter the 4 Set value = 3
Enter the 5 Set value = 2
tuple Items =  (6, 5, 4, 3, 2)
Tuple Length =  5
Maximum in Tuple =  6
Minimum in Tuple =  2
Sum of Tuple Elements =  20

0 Comments:

Post a Comment