Python program for linear search; In this tutorial, you will learn how to create linear search program in python.
First of all, A linear search, also known as a sequential search, this method is used to find an element within a list or array. It checks each element of the list one by one / sequentially until a match is found or the whole list has been searched.
Linear Search Algorithm
Implement linear search following the below steps:
- Traverse the list/array using a loop.
- In every iteration, associate the
target
value with the given value of the list/array.- If the values match, return the current index of the list/array.
- Otherwise, move on to the next array/list element.
- If no match is found, return
-1
.
Python Program for Linear Search
- Python Program For Linear Search Using While Loop
- Python Program For Linear Search Using For Loop
- Linear Search in Python Program using Recursion
Python Program For Linear Search Using While Loop
# python program for linear search using while loop
#define list
lst = []
#take input list size
num = int(input("Enter size of list :- "))
for n in range(num):
#append element in list/array
numbers = int(input("Enter the array of %d element :- " %n))
lst.append(numbers)
#take input number to be find in list
x = int(input("Enter number to search in list :- "))
i = 0
flag = False
while i < len(lst):
if lst[i] == x:
flag = True
break
i = i + 1
if flag == 1:
print('{} was found at index {}.'.format(x, i))
else:
print('{} was not found.'.format(x))
After executing the program, the output will be:
Enter size of list :- 5 Enter the array of 0 element :- 10 Enter the array of 1 element :- 23 Enter the array of 2 element :- 56 Enter the array of 3 element :- 89 Enter the array of 4 element :- 200 Enter number to search in list :- 89 89 was found at index 3.
Python Program For Linear Search Using For Loop
# python program for linear search using for loop
#define list
lst = []
#take input list size
num = int(input("Enter size of list :- "))
for n in range(num):
#append element in list/array
numbers = int(input("Enter the array of %d element :- " %n))
lst.append(numbers)
#take input number to be find in list
x = int(input("Enter number to search in list :- "))
i = 0
flag = False
for i in range(len(lst)):
if lst[i] == x:
flag = True
break
if flag == 1:
print('{} was found at index {}.'.format(x, i))
else:
print('{} was not found.'.format(x))
After executing the program, the output will be:
Enter size of list :- 6 Enter the array of 0 element :- 25 Enter the array of 1 element :- 50 Enter the array of 2 element :- 100 Enter the array of 3 element :- 200 Enter the array of 4 element :- 250 Enter the array of 5 element :- 650 Enter number to search in list :- 200 200 was found at index 3.
Linear Search in Python Program using Recursion
# python program for linear search using for loop
#define list
lst = []
#take input list size
num = int(input("Enter size of list :- "))
for n in range(num):
#append element in list/array
numbers = int(input("Enter the array of %d element :- " %n))
lst.append(numbers)
#take input number to be find in list
x = int(input("Enter number to search in list :- "))
# Recursive function to linear search x in arr[l..r]
def recLinearSearch( arr, l, r, x):
if r < l:
return -1
if arr[l] == x:
return l
if arr[r] == x:
return r
return recLinearSearch(arr, l+1, r-1, x)
res = recLinearSearch(lst, 0, len(lst)-1, x)
if res != -1:
print('{} was found at index {}.'.format(x, res))
else:
print('{} was not found.'.format(x))
After executing the program, the output will be:
Enter size of list :- 5 Enter the array of 0 element :- 14 Enter the array of 1 element :- 25 Enter the array of 2 element :- 63 Enter the array of 3 element :- 42 Enter the array of 4 element :- 78 Enter number to search in list :- 78 78 was found at index 4.
Recommended Python Programs
- Python Program to Add Two Numbers
- Python Program to Find/Calculate Sum of n Numbers
- Python Program to Find/Calculate Average of 3, 4, 5…n numbers
- Python Program to Print ASCII Value of Character
- Write a Program to Calculate Simple Interest in Python
- Python Program to Compute Compound Interest
- Leap Year Program in Python
- Python Program to Print Star Pattern
- Number Pattern Programs in Python
- Python Program to Print Even and Odd numbers From 1 to N
- Python Abs() Function: For Absolute Value
- How to Check Whether a Number is Fibonacci or Not in Python
- Python: Program to Find Power of Number
- Python Program to Reverse a Numbers
- Python Program to Find Smallest/Minimum of n Numbers
- Python Program to Find Largest/Maximum of n Numbers
- Python Program to Find The Net Bill Amount After Discount
- Python Program to Print Numbers From N to 1 and 1 to N
- Python Program to Print Numbers Divisible by 3, 5, 7
- Python Program to Print Prime Number 1 to N
- How to Find Square of Number in Python
- Python Program to Calculate Cube of Number
- Python Program to Find LCM of Two Numbers
- BMI (Body Mass Index) Calculator in Python
- Palindrome Program in Python using while loop, Function, etc
- Python: Program to Count Total Number of Bits in Number
- Python Random Number Generator Code
- Python Program to Calculate n-th term of a Fibonacci Series
- Zip Zap Zoom Python Program
- Python: program to convert Celsius to Fahrenheit
- Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
- Python: Program to Convert Decimal to Binary, Octal and Hexadecimal
- Python Program to Find Roots of Quadratic Equation
- Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
- Python Program to Check Given Input is Alphabet, Number or Special Character
- Python Program to Check IF a Number is Power of Another Number
- Python Check Binary Representation of Given Number is Palindrome or Not
- Python Program to Draw a Pie Chart
- Python Program Input the Radius of Circle and Compute the Area
- Python Program to Calculate the Area of a Rectangle
- Python Program to Calculate Area of Triangle
- Python Program to Find Area and Circumference of Circle using Radius
- Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
- Python Program to Print Binary Value of Numbers From 1 to N
- Python Program to Find Sum Of Even and Odd numbers From 1 to N
- Python Program to Calculate Simple and Compound Interest
- Python Program to Find Factors of a Number