Skip to content

Instantly share code, notes, and snippets.

@Edald123
Created Jul 20, 2021
Embed
What would you like to do?
Linear search in Python
number_list = [10, 14, 19, 26, 27, 31, 33, 35, 42, 44]
target_number = 33
def linear_search(search_list, target_value):
for idx in range(len(search_list)):
if search_list[idx] == target_value:
return idx
raise ValueError("{0} not in list".format(target_value))
try:
# Call the function below...
result = linear_search(number_list, target_number)
print("Linear search:", result)
except ValueError as error_message:
print("{0}".format(error_message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment