Easy Tutorial
❮ Python String Capitalize Ref Math Radians ❯

Python Linear Search

Python3 Examples

Linear search refers to checking each element of the array in a certain order until the desired specific value is found.

Example

def search(arr, n, x): 

    for i in range (0, n): 
        if (arr[i] == x): 
            return i 
    return -1 

# Searching for character D in array arr
arr = [ 'A', 'B', 'C', 'D', 'E' ] 
x = 'D' 
n = len(arr) 
result = search(arr, n, x) 
if(result == -1): 
    print("Element is not in the array") 
else: 
    print("Element is at the index", result)

Executing the above code outputs:

Element is at the index 3

Python3 Examples

❮ Python String Capitalize Ref Math Radians ❯