write a program for linear search in python.

 write a program for linear search in python.




def linear_search(arr, target):

    """

    Returns the index of target if found in arr, otherwise returns -1.

    """

    for i in range(len(arr)):

        # Check if the current element is the target

        if arr[i] == target:

            return i  # Target found at current index

            

    return -1  # Target not found in the entire list


# Example usage:

my_list = [10, 50, 30, 70, 80, 20]

target_value = 70


result = linear_search(my_list, target_value)


if result != -1:

    print(f"Target {target_value} found at index: {result}")

else:

    print(f"Target {target_value} not found in the list.")





Comments