Programming Examples

Python program to search for an element in a given list of numbers


WAP to search for an element in a given list of numbers.

Solution 1:

L=[2,58,95,999,65,32,15,1,7,45]
n=int(input("Enter the number to be searched : "))
if n in L: 
	print("Item found at the Position : ",L.index(n)+1)
else:
    print("Item not found in list")

Hear we use in operator of list which is used to check where an element is parent in list or not, and index function help to find the index of given element.

Solution 2:

L=[2,58,95,999,65,32,15,1,7,45]
n=int(input("Enter the number to be searched : "))
found=0 
for x in L:
    if x==n: 
	print("Item found at the Position : ",L.index(n)+1)
	found=1
if found==0:
    print("Item not found in list")

Output/ Explanation:

Enter the number to be searched : 6
Item not found in list

===============================

Enter the number to be searched : 15
Item found at the Position :  7


Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad