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

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


CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz