{"id":6727,"date":"2019-12-26T20:40:59","date_gmt":"2019-12-26T20:40:59","guid":{"rendered":"https:\/\/holypython.com\/?p=6727"},"modified":"2021-03-28T13:15:38","modified_gmt":"2021-03-28T13:15:38","slug":"linear-search-algorithm-python-code","status":"publish","type":"post","link":"https:\/\/holypython.com\/linear-search-algorithm-python-code\/","title":{"rendered":"Linear Search Algorithm (Python Code)"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"6727\" class=\"elementor elementor-6727\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-4aa78c95 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4aa78c95\" data-element_type=\"section\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-5acbd18b\" data-id=\"5acbd18b\" data-element_type=\"column\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-inner-section elementor-element elementor-element-6619ccd2 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"6619ccd2\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-50 elementor-inner-column elementor-element elementor-element-798ea2ce\" data-id=\"798ea2ce\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-63588234 elementor-view-default elementor-widget elementor-widget-icon\" data-id=\"63588234\" data-element_type=\"widget\" data-widget_type=\"icon.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-icon-wrapper\">\n\t\t\t<div class=\"elementor-icon\">\n\t\t\t<i aria-hidden=\"true\" class=\"fas fa-book-reader\"><\/i>\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t<div class=\"elementor-column elementor-col-50 elementor-inner-column elementor-element elementor-element-11c50a7b\" data-id=\"11c50a7b\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-71980e5e elementor-widget elementor-widget-heading\" data-id=\"71980e5e\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Algorithm: Linear Search<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<div class=\"elementor-element elementor-element-7f27e389 elementor-align-left elementor-icon-list--layout-traditional elementor-list-item-link-full_width elementor-widget elementor-widget-icon-list\" data-id=\"7f27e389\" data-element_type=\"widget\" data-widget_type=\"icon-list.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<ul class=\"elementor-icon-list-items\">\n\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"fas fa-code\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\"><b>FUNCTION<\/b>: Search<\/span>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"fas fa-plane-departure\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\"><b>PERFORMANCE<\/b>:      \u041e(n)<\/span>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"far fa-clock\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\"><b>FOUNDED IN<\/b>: Ancient<\/span>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item\">\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-icon\">\n\t\t\t\t\t\t\t<i aria-hidden=\"true\" class=\"far fa-user-circle\"><\/i>\t\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text\"><b>FOUNDED BY<\/b>: Anonymous<\/span>\n\t\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t\t\t<\/ul>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-03e1ca6 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"03e1ca6\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-78a86a6\" data-id=\"78a86a6\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-c2983c7 elementor-widget elementor-widget-text-editor\" data-id=\"c2983c7\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>One of the most common and basic algorithms, linear search goes through a collection and sequentially checks each item until the term being searched matches one of the items in the collection (such as a list).<\/p><p>Here is an illustration showing the search process of number 802:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<section class=\"elementor-section elementor-inner-section elementor-element elementor-element-1fc57f1 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"1fc57f1\" data-element_type=\"section\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-inner-column elementor-element elementor-element-08e85c2\" data-id=\"08e85c2\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-9d746d1 elementor-widget elementor-widget-heading\" data-id=\"9d746d1\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">STEP BY STEP<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<div class=\"elementor-element elementor-element-1d7e482 elementor-widget elementor-widget-image\" data-id=\"1d7e482\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t<figure class=\"wp-caption\">\n\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"1017\" height=\"221\" src=\"https:\/\/holypython.com\/wp-content\/uploads\/2019\/12\/Linear-Search-scheme.jpg\" class=\"attachment-large size-large wp-image-6730\" alt=\"\" srcset=\"https:\/\/holypython.com\/wp-content\/uploads\/2019\/12\/Linear-Search-scheme.jpg 1017w, https:\/\/holypython.com\/wp-content\/uploads\/2019\/12\/Linear-Search-scheme-300x65.jpg 300w, https:\/\/holypython.com\/wp-content\/uploads\/2019\/12\/Linear-Search-scheme-768x167.jpg 768w\" sizes=\"(max-width: 1017px) 100vw, 1017px\" \/>\t\t\t\t\t\t\t\t\t\t\t<figcaption class=\"widget-image-caption wp-caption-text\">Linear Search Algorithm step by step<\/figcaption>\n\t\t\t\t\t\t\t\t\t\t<\/figure>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b0b0285 elementor-widget elementor-widget-text-editor\" data-id=\"b0b0285\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Below, you can see a simple Python code showing a linear search algorithm implementation.<\/p><p>Algorithms can be so much fun indeed. If you&#8217;ve started to learn coding recently it can be very fulfilling to fully understand an algorithm and code it on your own.<\/p><p>This algorithm is very suitable for a new programmer to build from scratch and if you have time and interest you are definitely encouraged to do so. Enjoy!<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<section class=\"elementor-section elementor-inner-section elementor-element elementor-element-6cc4b1f elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"6cc4b1f\" data-element_type=\"section\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-inner-column elementor-element elementor-element-ca0310e\" data-id=\"ca0310e\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-6b28bb9 elementor-widget elementor-widget-heading\" data-id=\"6b28bb9\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">LINEAR SEARCH ALGORITHM CODE IN PYTHON<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<div class=\"elementor-element elementor-element-7bf1d57 elementor-widget elementor-widget-html\" data-id=\"7bf1d57\" data-element_type=\"widget\" data-widget_type=\"html.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<pre rel=\"PYTHON\"><code>#Linear Search Algorithm\r\n\r\nlst = [44, 2, 13,52, 802, 44]\r\n\r\ndef lin_search(x):\r\n    for i in range(len(lst)):\r\n        if lst[i] == x:\r\n            return \"Your term's index is: \"+str(i)\r\n    if not found:\r\n        return \"Your search term can not be found.\"\r\n        \r\nprint(lin_search(802))\r\n<\/code><\/pre>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-22d64cd elementor-widget elementor-widget-text-editor\" data-id=\"22d64cd\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Your term&#8217;s index is: 4<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fcf3221 elementor-widget elementor-widget-text-editor\" data-id=\"fcf3221\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Linear Search Algorithm above also demonstrates so many fundamental concepts in Python such as: <a href=\"https:\/\/holypython.com\/beginner-python-lessons\/lesson-6-python-lists\/\">Lists<\/a>, <a href=\"https:\/\/holypython.com\/intermediate-python-lessons\/lesson-8-python-for-loop\/\">For Loops<\/a>, <a href=\"https:\/\/holypython.com\/beginner-python-lessons\/lesson-16-defining-functions\/\">User-defined Functions<\/a>, <a href=\"https:\/\/holypython.com\/beginner-python-lessons\/lesson-4-type-conversion\/\">Type Conversion<\/a>, <a href=\"https:\/\/holypython.com\/beginner-python-lessons\/lesson-16-defining-functions\/\">return statement<\/a>, <a href=\"https:\/\/holypython.com\/intermediate-python-lessons\/lesson-7-python-conditional-statements\/\">Conditional Statement (if)<\/a> and <a href=\"https:\/\/holypython.com\/beginner-python-lessons\/lesson-10-len\/\">len() function<\/a>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-cbb308a elementor-widget elementor-widget-text-editor\" data-id=\"cbb308a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Linear Search is also known as sequential search and is a quite useful algorithm beside being very simple to implement. It&#8217;s shortcoming becomes visible when used on large datasets in which case other search algorithms will likely be more useful.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Algorithm: Linear Search FUNCTION: Search PERFORMANCE: \u041e(n) FOUNDED IN: Ancient FOUNDED BY: Anonymous One of the most common and basic algorithms, linear search goes through a collection and sequentially checks each item until the term being searched matches one of the items in the collection (such as a list). Here is an illustration showing the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6730,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[45,47],"tags":[],"class_list":["post-6727","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithms","category-search"],"acf":[],"_links":{"self":[{"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/posts\/6727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/comments?post=6727"}],"version-history":[{"count":0,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/posts\/6727\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/media\/6730"}],"wp:attachment":[{"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/media?parent=6727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/categories?post=6727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/holypython.com\/wp-json\/wp\/v2\/tags?post=6727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}