Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Python Program to perform Linear Search

To write a Python Program to perform Linear Search 

Python
def linear_search(alist, key):
    """Return index of key in alist. Return -1 if key not present."""
    for i in range(len(alist)):
        if alist[i] == key:
            return i
    return -1

# Input and conversion
alist = input('Enter the list of numbers: ')
alist = alist.split()
alist = [int(x) for x in alist]

key = int(input('Enter the number to search for: '))

# Perform linear search
index = linear_search(alist, key)

# Display result
if index < 0:
    print('{} was not found.'.format(key))
else:
    print('{} was found at index {}.'.format(key, index))

OUTPUT:
Enter the list of numbers: 2 3 4 5 6 8 7

The number to search for: 4

4 was found at index 2.