Code in Python
Your How-to Guide

Linear Search in Python

23rd July 2017

This code snippet below will show you how to perform linear search in python. Creating a linear search program in python will touch the concepts in python like lists, indexing, for/while loops in python, conditional statements like if-elif-else. Follow us on Facebook, Twitter and Github to stay updated.

# ************LINEAR SEARCH**************

# asking for input of array from user

arr = []
num = int(input("enter number of elements: "))
for i in range(0, num):
    arr.insert(i, int(input("enter element: ")))

# searching element

item = int(input("enter element to be searched: "))
position = 0
while position <= num:              
    if item == arr[position]:
        print("element is at ", position + 1, " position")
    elif item not in arr:
        print("element not found")
    position += 1

About Author

Akarsh Singh

Developer, Tech. Evangalist, Loves to read and write about new tech. Madly in love with Python. Follow him on social media.

1. For more Awesome ads-free content help me to stay awake late. Buy me a coffee.

2. Want to know where Code in Python website is hosted. Buy cheapest Web hosting (less than 1.5$/month or Rs.99/month) using this referral link. This is trusted and tested web hosting service Code in Python relies on.

backupWeb hosting
3. Support Akarsh Singh(Founder, Code in Python) so that he can teach you more Python and Machine Learning Concepts for free. Contribute to his page and he will mention you in his next post.

4. Indian Users can donate through Paytm.