Python program for linear search; Through this tutorial, i am going to show you how to implement program for linear search in python using for loop, while loop and recursion function.

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.

In this tutorial, i will write a python programs to perform a linear search with list using for loop, while loop and recursion function.

## 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 using recursion

### python program for linear search using while loop

See the following python program for linear search using while loop; as shown below:

# 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 program in python 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 Tutorials

**Recommended:-**Python Insert Element at Specified Index in List

**Recommended:-**Python Program to Swap Two Elements in a List

**Recommended:-**How to Take Input List From User in Python

**Recommended:-**Python Program to Find LCM of Two Numbers

**Recommended:-**Python Add and Remove Elements From List

**Recommended:-**Python Find Difference of Two Lists

**Recommended:-**Python Program to Split Even and Odd Numbers in List

**Recommended:-**Python Program Count vowels in String

**Recommended:-**Python Program to Find HCF or GCD of Two Numbers

**Recommended:-**Python Program to find Sum of N Natural Numbers

**Recommended:-**Python Program to Find the Square Root of Number

**Recommended:-**Python Program to Check Armstrong Number

**Recommended:-**Python Program to Find Factors of a Number

**Recommended:-**Python Program for Binary Search

**Recommended:-**Python remove a character from a string

**Recommended:-**Python String to Int() and Int to String