# Algorithm learned with Python 9th: Linear search

# #Algorithm learned in Python

## Introduction

Implement the basic algorithm in Python to deepen your understanding of the algorithm.
As the ninth bullet, we deal with linear search.

## Linear search

Search: Finding the data you want from a lot of data
Linear: Examine in order from the beginning

In other words, linear search does not do anything complicated, but searches from the front in order.

## Programmatic linear search

Store the data in the list and check it in order from the beginning.
This is a very simple program structure and easy to implement
⇒ ** Effective method when the number of data is small **

## Implementation

The code of the linear search and the output at that time are shown below.

##### code

`linear_search.py`

```
"""
2020/12/21
@Yuya Shimizu
Linear search
"""
#Linear search function
def linear_search(data, target):
for i in range(len(data)):
if data[i] == target:
return i
return False
if __name__ == '__main__':
data = [50, 20, 70, 60, 40, 90, 30] #Target data to be searched
target = 40 #Value to search
found = linear_search(data, target)
if not found:
print(f"{target} is not found in the data")
else:
print(f"{target} is found at data[{found}]")
```

##### output

```
40 is found at data[4]
```

Next, the output result when False is returned is also shown.

##### output

```
45 is not found in the data
```

## A brief description of the linear search function

It is turned by the `for`

statement, but since we want to return the index of the data as the return value, we dare to use`for i in range (len (data))`

. Also, by `return`

in` for`

, it can be expressed by simply writing `return False`

outside the` for`

statement that the search result cannot be found.

## Impressions

Linear search is very simple and overlaps with intuition. I think it will be the basis for other searches. I am looking forward to learning from now on.

## References

Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms
Written by Toshikatsu Masui, Shoeisha