Write the python program to find an element from the array. In this Python Programming example, let’s see How to perform a linear search in Python or find the element from an array using a different method.
Algorithm
- Take input from the user, declare a vacant list
- Using loop to take input and append in list
- Check the condition
- Print the output
Python program to find an element from the array
a=int(input("enter size of array:")) lst=[] for i in range(a): b=int(input("enter element:")) lst.append(b) print(lst) c=int(input("enter the element to search:")) for i in range(a): if lst[i]==c: print("element is",lst[i]) print("at index",i) print("at position",i+1)
Explanation of the code
Step 1 taking input of size of the array
Step 2 declaring the list to use further use
Step 3 for loop to run the same code for the given number of times
Step 4 take input of element
Step 5 appending the element into the list
Step 6 printing the list
Step 7 taking another input to check the element
Step 8 for loop to access the element again
Step 9 checking the element and the accessed element again
Step 10 printing the element
Step 11 printing the index of the element
Step 12 printing the position of the element
Output
In this blog, we had learned how to find an element from an array using simple linear search algorithm