Python program for linear search using list and for loop. Python program first takes the list of all elements as input. After that input element to be found. If the element is present, it shows Element present else not found. This python project is useful for beginners and CBSE KV School Class 11 and Class 12 students computer science practical file and NIELIT O Level module.
WAP to input a list of number and search for a given number using linear search
Python code of program
Explanation of Source code in program
l=eval(input(“Enter the list of numbers”)) # inputs the list of numbers
x=int(input(“Enter the number”)) #inputs the number you want to check
for i in l: # here for loop starts from the first number of the list to the last number of the list
if i==x: # If any of the numbers matching the input to be checking number
print(“Element present”)# If the element is there in list then it shows element present
break # condition is breaking means program is terminating here if element is present
print(“Element not found”) # if it is not present there then it shows Element not Founding.
Output Linear search in python using list
Testing of program
In above shell window, we entered sets of numbers and asked to search for 54. Element is present, therefore out put is Element Present. We test this program with 2 more sets of elements.
Our python program is working.
There are three input with list showing output.