Python Program to Implement Linear Search














































Python Program to Implement Linear Search



"""Description :- Given a list L containing n elements. We are asked to find position/index an element k if it's present in list L solution: 1. Form the list as user's input. 2. Ask the user the element to be searched (key element). 3. Set boolean variable 'found' as False. 4. Run a for loop i in range from 0 to length of L i.e. n . 5. Using if command check if key element is equal to L[i]. 6. If yes : change 'found' to True print the index L[i] break the loop 7. If boolean variable 'found' is still False then print key element not found. """ L = [] #assigning an empty list n = input("Enter size of list: ") #input for length of list for i in range(n): num = input(" Enter any number: ") L.append(num) #adding elements to list k = input("\nEnter number to search: ") #\n is just used for adding new line found = False #setting boolean variable as False for i in range(len(L)): if L[i] == k: found = True print k," found at position ", i break #to stop the search if not found: #not False = True print k," is not in list" """Input: Enter size of list:3 Enter any number: 1 Enter any number: 2 Enter any number: 3 Enter number to search: 5 Output: 5 is not in list """

More Articles of Priyanshi Deshmukh:

Name Views Likes
Python Program to Implement Linear Search 387 30

Comments