Ticker

6/recent/ticker-posts

Python case study subjective question for competitive exam (3)

Q1.How you can implement a function to add student records to the student databases using python list.write the python code to achieve this,considering that each student record consists of a unique student id,firstname,lastname and age.

student=[] def add_student(id,fname,lname,age): for record in student: if record['id']==id: print("id already exit") return -1 newstudent={'id':id,'fname':fname,'lname':lname,'age':age} student.append(newstudent) c='y' while c in 'y': id=int(input("enter id")) fname=input("enter first name") lname=input("enter last name") age=int(input("enter age")) add_student(id,fname,lname,age) c=input("do you want to add more student press y otherwise n") for i in student: print(i) After run module output is == RESTART: C:/Users/Dell/AppData/Local/Programs/Python/Python37-32/a22.py == enter id 1 enter first name ram enter last name kumar enter age 12 do you want to add more student press y otherwise ny enter id 1 enter first name sham enter last name kumar enter age12 id already exit do you want to add more student press y otherwise ny enter id 2 enter first name sham enter last name kumar enter age 12 do you want to add more student press y otherwise nn {'id': 1, 'fname': ' ram', 'lname': ' kumar', 'age': 12} {'id': 2, 'fname': ' sham', 'lname': ' kumar', 'age': 12}

Q2.Implement a function to search for a student by their id in the student database using python list.write the python code to perform the search operation and return the student details if found or display a message if the student is not present in the database.

student=[] def add_student(id,fname,lname,age): for record in student: if record['id']==id: print("id already exit") return -1 newstudent={'id':id,'fname':fname,'lname':lname,'age':age} student.append(newstudent) c='y' while c in 'y': id=int(input("enter id")) fname=input("enter first name") lname=input("enter last name") age=int(input("enter age")) add_student(id,fname,lname,age) c=input("do you want to add more student press y otherwise n") for i in student: print(i) res=-1 id=int(input("enter id you want to search")) for record in student: if record['id']==id: res=id break if res==-1: print("not found") else: print("found") Output >>> == RESTART: C:/Users/Dell/AppData/Local/Programs/Python/Python37-32/a22.py == enter id1 enter first nameram enter last namesharma enter age23 do you want to add more student press y otherwise ny enter id2 enter first namesham enter last namesharma enter age23 do you want to add more student press y otherwise nn {'id': 1, 'fname': 'ram', 'lname': 'sharma', 'age': 23} {'id': 2, 'fname': 'sham', 'lname': 'sharma', 'age': 23} enter id you want to search2 found



Q3.Develop a function to update the age of a student in the student database using python list.write the python code to perform the update operation given the student id and the new age value

student=[] def add_student(id,fname,lname,age): for record in student: if record['id']==id: print("id already exit") return -1 newstudent={'id':id,'fname':fname,'lname':lname,'age':age} student.append(newstudent) c='y' while c in 'y': id=int(input("enter id")) fname=input("enter first name") lname=input("enter last name") age=int(input("enter age")) add_student(id,fname,lname,age) c=input("do you want to add more student press y otherwise n") for i in student: print(i) res=-1 id=int(input("enter id you want to search")) for record in student: if record['id']==id: res=0 record['age']=age+10 break if res==-1: print("not found") for i in student: print(i) Output >>> == RESTART: C:/Users/Dell/AppData/Local/Programs/Python/Python37-32/a22.py == enter id1 enter first nameram enter last namesham enter age10 do you want to add more student press y otherwise ny enter id2 enter first namesham enter last namesharma enter age10 do you want to add more student press y otherwise nn {'id': 1, 'fname': 'ram', 'lname': 'sham', 'age': 10} {'id': 2, 'fname': 'sham', 'lname': 'sharma', 'age': 10} enter id you want to search2 {'id': 1, 'fname': 'ram', 'lname': 'sham', 'age': 10} {'id': 2, 'fname': 'sham', 'lname': 'sharma', 'age': 20}

Q4.Develop a function to remove a student record in the student database using python list.write the python code to perform the remove operation given the student id and the new age value



student=[] def add_student(id,fname,lname,age): for record in student: if record['id']==id: print("id already exit") return -1 newstudent={'id':id,'fname':fname,'lname':lname,'age':age} student.append(newstudent) c='y' while c in 'y': id=int(input("enter id")) fname=input("enter first name") lname=input("enter last name") age=int(input("enter age")) add_student(id,fname,lname,age) c=input("do you want to add more student press y otherwise n") for i in student: print(i) res=-1 id=int(input("enter id you want to search")) for record in student: if record['id']==id: res=0 student.remove(record) break if res==-1: print("not found") for i in student: print(i) output >>> == RESTART: C:/Users/Dell/AppData/Local/Programs/Python/Python37-32/a22.py == enter id1 enter first namereema enter last namesharma enter age12 do you want to add more student press y otherwise ny enter id2 enter first namenitin enter last namesharma enter age23 do you want to add more student press y otherwise nn {'id': 1, 'fname': 'reema', 'lname': 'sharma', 'age': 12} {'id': 2, 'fname': 'nitin', 'lname': 'sharma', 'age': 23} enter id you want to search2 {'id': 1, 'fname': 'reema', 'lname': 'sharma', 'age': 12} >>>

Q5.Develop a function to update the age of all student in the student database using python list.write the python code to perform the update operation.



student=[] def add_student(id,fname,lname,age): for record in student: if record['id']==id: print("id already exit") return -1 newstudent={'id':id,'fname':fname,'lname':lname,'age':age} student.append(newstudent) c='y' while c in 'y': id=int(input("enter id")) fname=input("enter first name") lname=input("enter last name") age=int(input("enter age")) add_student(id,fname,lname,age) c=input("do you want to add more student press y otherwise n") for i in student: i['age']=i['age']+10 print(i) Output >>> === RESTART: C:/Users/Dell/AppData/Local/Programs/Python/Python37-32/k.py === enter id1 enter first nameram enter last namesharma enter age12 do you want to add more student press y otherwise ny enter id2 enter first namesham enter last namearora enter age10 do you want to add more student press y otherwise nn {'id': 1, 'fname': 'ram', 'lname': 'sharma', 'age': 22} {'id': 2, 'fname': 'sham', 'lname': 'arora', 'age': 20}



Post a Comment

0 Comments