WAP to search an element from a list of numbers.
Wap: write a program
Using python
Answers
Answer
Here is the answer-
# Search function with parameter list name
# and the value to be searched
def search(list,n):
for i in range(len(list)):
if list[i] == n:
return True
return False
# list which contains both string and numbers.
list = [1, 2, 'sachin', 4,'Geeks', 6]
# Driver Code
n = 'Geeks'
if search(list, n):
print("Found")
else:
print("Not Found")
# I'll be using 3 variables for this program, i.e., "a", "size", "element".
a=eval(input("Enter the numbers for the list separated by comma(,) : ")) # Without the square parenthesis the input number will be converted to tuple. So to make it a list we can use the function list().
a=list(a) # Converting the tuple into list.
element=int(input("Enter element to be searched for : ")) # This variable will help use find what the user needs.
for x in range(0,size):
if element ==a[x]:
print(element, "found at index", x)
break
else:
print(element, "not found in the given list.")