Q1. Write a python program to count the number of characters in a string without using any built-in function.
def findLength(string):
= 0
count
for i in string:
+= 1
count return count
= input("Enter The String")
string print("NUmber of Characters in string are",findLength(string))
OUTPUT
Enter The StringRahul Sonawane
NUmber of Characters in string are 14
Q2. Define a class Person having members – name, address. Create a subclass called ―Employee with member staffid, salary. Create ‘n’ objects of the Employee class and display all the details of highest salaried employee.
class Employee:
def AcceptEmp(self):
self.Id=int(input("Enter emp id:"))
self.Name=input("Enter emp name:")
self.Dept=input("Enter emp Dept:")
self.Sal=int(input("Enter emp Salary:"))
def DisplayEmp(self):
print("Emp id:",self.Id)
print("Emp Name:",self.Name)
print("Emp Dept:",self.Dept)
print("Emp Salary:",self.Sal)
class Manager(Employee):
def AcceptMgr(self):
self.bonus=int(input("Enter Manager Bonus"))
def DisplayMgr(self):
print("Manger Bonus is:",self.bonus)
self.TotalSal=self.Sal+self.bonus
print("Total Salary: ", self.TotalSal)
=int(input("Enter How may Managers:"))
n=[]
sfor i in range(0,n):
=input("Enter Object Name:")
x
s.append(x)print(s)
for j in range(0,n):
=Manager()
s[j]
s[j].AcceptEmp()
s[j].AcceptMgr()for k in range(0,n):
print("\nDisplay Details of Manager",k+1)
s[k].DisplayEmp()
s[k].DisplayMgr()
= s[0].TotalSal
maxTotalSal=0
maxIndexfor j in range(1,n):
if s[j].TotalSal > maxTotalSal:
= s[j].TotalSal
maxTotalSal=j
maxIndexprint("\nMaximum Salary(Salary+Bonus)")
s[maxIndex].DisplayEmp()
s[maxIndex].DisplayMgr()
#Check OutPut on Slip 11
Q2. Write a python program to check if a given key already exists in a dictionary. If key exists replace with another key/value pair.
def checkKey(dic, key):
print("Given Dictionary is:-", dic);
if key in dic.keys():
print("Key Present, ", end =" ")
print("value =", dic[key])
del(dic[key])
=(input("Enter New Key"))
k=int(input("Enter New Value"))
v=v
dic[k]print("New Dictionary is:-", dic);
else:
print("Key Not present")
= {'a': 100, 'b':200, 'c':300}
dic = 'b'
key checkKey(dic, key)
OUTPUT
Given Dictionary is:- {'a': 100, 'b': 200, 'c': 300}
Key Present, value = 200
Enter New Keyk
Enter New Value101
New Dictionary is:- {'a': 100, 'c': 300, 'k': 101}
0 Comments:
Post a Comment