Q16: Write a python code to input a number and perform linear search or search
in a list.
Ans:
list1= [10, 20, 30,40,10,50,10]
element=int(input("Enter element to count:"))
l=len(list1)
for i in range(l):
if list1[i]== element:
print(element, "found at index",i)
break
else:
print(element," not present in the given list")
Q11: Writing Python program to add multiple record in a binary file.
Ans:
import pickle
def write():
file=open('demo.txt','ab')
y='y'
while y=='y':
roll=int(input('Enter roll no.:'))
name=input('Enter name:')
marks=int(input('Enter Marks:'))
data=[roll,name,marks]
pickle.dump(data,file)
y=input('Do you want to enter data again:')
file.close()
Q13. Write a function push() and pop() to add a new student name and
remove a student name from a list student, considering them to act as
PUSH and POP operations of stack Data Structure in Python.
st=[ ]
def push():
y="y"
while y=="y":
sn=input("Enter name of student:")
st.append(sn)
y=input("Do you want to enter more name(y/n):")
def pop():
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())
Output:-
Q14. Write a function Push() which takes "name" as argument and add in a
stack named "MyStack". After calling push() three times, a message
should be displayed "Stack is Full"
st=[ ]
StackSize=3
def push():
y="y"
while y=="y":
sn=input("Enter name of student:")
if len(st)<StackSize:
st.append(sn)
else:
print("Stack is full!")
print(st)
break
y=input("Do you want to enter more name(y/n):")
Output:-
Q15: Julie has created a dictionary containing names and marks as key
value pairs of 6 students. Write a program, with separate user defined
functions to perform the following operations:
Push the keys (name of the student) of the dictionary into a stack,
where the corresponding value (marks) is greater than 75.
Pop and display the content of the stack.
R={"OM":76, "JAI":45, "BOB":89,"ALI":65, "ANU":90, "TOM":82}
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
ST=[]
for k in R:
if R[k]>75:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
Output: