• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » Linear Search in Python
Next →
← Prev

Linear Search in Python

By Dinesh Thakur

In this tutorial, we will learn the linear search in Python. we will write a code to search an element from a list. It compares each element to the criterion that we are finding. If all tiles are there, the element is located, and the algorithm returns the key’s index location.

so let’s see if you have a list and we have values let’s say

[5 ,8, 4 ,6, 9 ,2]

Suppose you want to search for a particular value within a list and search, for example, nine. How do we do that? We do have specific functions in Python using which you can do that, but what if you want to do it by yourself? You want to do it manually using a general loop. If you wish to search for an element, there are two scenarios here.

• The first scenario is the element that is searching for is in the list.
• The second scenario is the element that is searching for is not in the list.

Let’s go with the best scenario where you’re searching for nine, and it is in the list. Now how do we check? We have to run a loop, and you will check for each element.

Let’s say if you’re searching for nine, you will compare nine with the list’s first element, which is five in this case. Since it is not matching, you will go for the next element. We’ll compare that if this is not matching you will go for the next element that’s what you do and usually, do this till you find the element which is nine itself, which is the fifth element in the array with an index value for of course or the least, so let’s do it, so the element which you’re searching for is nine.

Let’s run through the method to decide the element key = 9 .

Linear Search in Python

We’ll be covering the following topics in this tutorial:

  • Python Linear Search Algorithm
  • Python Program for Linear Search
  • Python Linear Search Complexity

Python Linear Search Algorithm

There is list of n elements and key value to be searched.

Below is the linear search algorithm.

Search(list, n) 
while i<len(list):
   if list[i] == n: 
     return its index position 
   return -1 

Let’s we are implementing, and in this search, you will pass two things so you will pass a list and pass our values that are searching for if the value exists, you will print found else you will print not found.

Python Program for Linear Search

pos = -1
def search(list,n):
    i = 0
    while i<len(list):
      if list[i] == n:
        globals()['pos'] = i
        return True
      i = i + 1
    return False
list = [5,8,4,6,9,2]
n = 9
if search(list,n):
   print("Found at ",pos+1)
else:
   print("Not Found")

Output:

Program of Linear Search in PythonPython Linear Search Complexity

Time complexity of linear search algorithm

• Base Case – O(1)
• Average Case – O(n)
• Worst Case – O(n)

You’ll also like:

  1. What is Linear Search
  2. Array C++ Linear Search
  3. What is linear search in C language?
  4. What is Python? | Introduction to Python Programming
  5. Python Features | Main Features of Python Programming Language
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW