Python 3 Linear Search Algorithm Script to Find Number Position in Array Full Project For Beginners

You are currently viewing Python 3 Linear Search Algorithm Script to Find Number Position in Array Full Project For Beginners

 

 

Welcome folks today in this blog post we will be finding number position in array using linear search algorithm in python. All the full source code of the application is shown below.

 

 

 

 

Get Started

 

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

# Searching an element in a list/array in python
# can be simply done using \'in\' operator
# Example:
# if x in arr:
# print arr.index(x)

# If you want to implement Linear Search in python

# Linearly search x in arr[]
# If x is present then return its location
# else return -1

def search(arr, x):

    for i in range(len(arr)):

        if arr[i] == x:
            return i

    return -1

print(search([45,56,67,78],67))

 

 

Now if you execute the python script by typing the below command as shown below

 

 

python app.py

 

 

Leave a Reply