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
=input("Enter First String:-\t")
a
=input("Enter Second String:-\t")
b
=a[0:2]
x
=a.replace(a[0:2],b[0:2])
a
=b.replace(b[0:2],x)
b
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))
=int(input("Enter How may Employees:"))
n=[]
sfor i in range(0,n):
=input("Enter Object Name:")
x
s.append(x)print(s)
for j in range(0,n):
print("\nEnter Details of Employee",j+1)
=input('enter name : ')
name1=input('enter address : ')
address=int(input('enter salary : '))
salary=employee(name1,address,salary)
s[j]
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
= tuple()
tup =int(input("Enter the Number of Elements in the Tuple\t"))
nfor i in range(1, n+1):
= int(input("Enter the %d Set value = " %i))
value =tup+(value,)
tupprint("tuple Items = ", tup)
=0
length= 0
maximum= math.inf #Set the initial min to infinity
minimum=0
sumtupfor i in tup:
+= 1
length+= i
sumtupif maximum < i:
= i
maximum if minimum > i:
= i
minimum 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