In this tutorial, we will introduce the way to implement a linear search using python.
What is linear search?
Linear Search a search algorithm that is used to find an element within a list or array. It will check each element of the list one by one until a match is found or the whole list has been searched.
How to implement linear search in python?
1. You should create a list which contains some elements
lst = [1, 2, 3, 4, 5]
2. Find a number using linear search
num = 3 x = -1 for i in lst: x += 1 if num == i: print(x) print(str(-1))
If we have found num in lst, we will print the index, otherwise, we will print -1.