Skip to content

Instantly share code, notes, and snippets.

@Edald123
Created July 20, 2021 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Edald123/64695796eece5666408c09d7f9c12292 to your computer and use it in GitHub Desktop.
Save Edald123/64695796eece5666408c09d7f9c12292 to your computer and use it in GitHub Desktop.
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