Python Tutorial for Beginners: Binary Search Using Python. Python Program for Binary Search: You will learn how the Binary Search algorithm works behind the scenes and how you can implement it in Python. Why it is a very efficient algorithm compared to Linear Search. How its Python implementation works line by line.

WelcomeIn this article, you will learn how the Binary Search algorithm works behind the scenes and how you can implement it in Python.

**In particular, you will learn:**

- How the algorithm works behind the scenes to find a target element.
- How its Python implementation works line by line.
- Why it is a very efficient algorithm compared to Linear Search.
- Its advantages and requirements.

**Let's begin! β¨**

This algorithm is used to find an element in an ordered sequence (for example: a list, tuple, or string).

To apply the Binary Search algorithm to a sequence, the sequence already has to be sorted in ascending order. Otherwise, the algorithm will not find the correct answer. If it does, it will be by pure coincidence.

**π‘ Tip:** You can sort the sequence before applying Binary Search with a sorting algorithm that meets your needs.

The algorithm (implemented as a function) needs this data:

- An ordered sequence of elements (for example: list, tuple, string).
- The target element that we are searching for.

It returns the **index** of the element that you are looking for if it's found. If the element is not found, -1 is returned.

It is very efficient compared to Linear Search (searching for an element one by one, starting from the first one) because we are able to "discard" half of the list on every step.

Let's start diving into this algorithm.

πΉ Visual WalkthroughWe will apply the Binary Search algorithm to this list:

π‘ **Tip:** Notice that the list is already sorted. It included the indices as a visual reference.

We want to find the index of the integer **67**.

Let's pretend that we are the algorithm. How do we start the process?

We start by selecting the two bounds of the interval where we want to search. We want to search the entire list, so we select index `0`

as the lower bound and index `5`

as the upper bound:

Now we need to find the index of the middle element in this interval. We do this by adding the lower bound and the upper bound and dividing the result by 2 using integer division.

In this case, `(0 + 5)//2`

is ** 2** because the result of

`5/2`

is `2.5`

and integer division truncates the decimal part.So the middle element is located at **index 2**, and the middle element is the number **6**:

Now we need to start comparing the middle element with our target element to see what we need to do next.

We ask:

**Is the middle element equal to the element that we are looking for?**

```
6 == 67 # False
```

No, it isn't.

So we ask:

**Is the middle element greater than the element that we are looking for?**

```
6 > 67 # False
```

No, it isn't.

So **the middle element is smaller than the element that we are looking for.**

```
6 < 67 # True
```

Since the list is already sorted, this tells us something extremely important. It tells us that we can "discard" the lower half of the list because we know that all the elements that come before the middle element will be smaller than the element that we are looking for, so our target element is not there.

What do we do next? We've discarded the elements and the cycle is repeated again.

We have to choose the bounds for the new interval (see below). But notice that the upper bound is kept intact and only the lower bound is changed.

This is because the element that we are looking could be in the upper half of the list. The upper bound is kept intact and the lower bound is changed to "shrink" the interval to an interval where our target element could be found.

π‘ **Tip:** If the middle element had been greater than the element that we are looking for, the upper bound would have been changed and the lower bound would have been kept intact. This way, we would discard the upper half of the list and continue searching in the lower half.

Now we need to find the index of the middle element by adding the lower bound to the upper bound and dividing the result by 2 using integer division.

The result of `(3+5)//2`

is `4`

, so the middle element is located at **index** `**4**`

and the middle element is **67**.

We ask:

**Is the middle element equal to the element that we are looking for?**

```
67 == 67 # True
```

Yes, it is! So we've found the element at index **4**. The value 4 is returned and the algorithm was completed successfully.

π‘ **Tip:** If the element had not been found, the process would have continued until the interval was no longer valid. If the element had not been found in the entire list, -1 would have been returned.

Now that you have a visual intuition of how the algorithm works behind the scenes, let's dive into the iterative Python implementation by analyzing it line by line:

```
def binary_search(data, elem):
low = 0
high = len(data) - 1
while low <= high:
middle = (low + high)//2
if data[middle] == elem:
return middle
elif data[middle] > elem:
high = middle - 1
else:
low = middle + 1
return -1
```

Here we have the function header:

```
def binary_search(data, elem):
```

It takes two arguments:

- The ordered sequence of elements (for example: list, tuple, or string).
- The element that we want to find.

The next line sets the initial lower and upper bounds:

```
low = 0
high = len(data) - 1
```

The initial lower bound is index `0`

and the initial upper bound is the last index of the sequence.

We will repeat the process while there is a valid interval, while the lower bound is smaller than or equal to the upper bound.

```
while low <= high:
```

π‘ **Tip:** Remember that the bounds are indices.

On every iteration, we need to find the index of the middle element. To do this, we add the lower and upper bounds and divide the result by 2 using integer division.

```
middle = (low + high)//2
```

π‘ **Tip:** We use integer division in case the list or interval contains an even number of elements. For example, if the list had 6 elements and we did not use integer division, `middle`

would be the result of `(0 + 5)/2`

which is `2.5`

. An index cannot be a float, so we truncate the decimal portion by using `//`

and select the element at index `2`

.

With these conditionals (see below), we determine what to do depending on the value of the middle element `data[middle]`

. We compare it to the target element that we are looking for.

```
if data[middle] == elem:
return middle
elif data[middle] > elem:
high = middle - 1
else:
low = middle + 1
```

There are three options:

- If the middle element is equal to the element that we are looking for, we return the index immediately because we found the element.

```
if data[middle] == elem:
return middle
```

- If the middle element is greater than the element that we are looking for, we reassign the upper bound because we know that the target element is in the lower half of the list.

```
elif data[middle] > elem:
high = middle - 1
```

- Else, the only option left is that the middle element is smaller than the element that we are looking for, so we reassign the lower bound because we know that the target element is in the upper half of the list.

```
else:
low = middle + 1
```

If the loop is completed without finding the element, the value -1 is returned.

```
return -1
```

and we have the final implementation of the Binary Search algorithm:

```
def binary_search(data, elem):
low = 0
high = len(data) - 1
while low <= high:
middle = (low + high)//2
if data[middle] == elem:
return middle
elif data[middle] > elem:
high = middle - 1
else:
low = middle + 1
return -1
```

πΉ Special Cases
These are some particular cases that you may find as you start working with this algorithm:

If the element that you are looking for is repeated in the sequence, the index returned will depend on the number of elements and on the sequence of operations that the algorithm performs on the sequence.

```
>>> >>> b = [2, 2, 3, 6, 7, 7]
>>> binary_search(b, 7)
4
```

If the element is not found, -1 is returned.

```
>>> b = [2, 2, 3, 6, 7, 7]
>>> binary_search(b, 8)
-1
```

If the sequence is empty, -1 will be returned.

```
>>> b = []
>>> binary_search(b, 8)
-1
```

If the sequence is unsorted, the answer will not be correct. Getting the correct index is pure coincidence and it could be due to the order of the elements in the sequence and the sequence of operations performed by the algorithm.

This example returns the correct result:

```
>>> b = [5, 7, 3, 0, -9, 2, 6]
>>> binary_search(b, 6)
6
```

But this one doesn't:

```
>>> b = [5, 7, 3, 0, -9, 2, 10, 6]
>>> binary_search(b, 6)
-1
```

π‘ **Tip:** Think about why the first example returns the correct result. Hint: It's pure coincidence that the order of the elements happens to make the algorithm reach the correct index, but the step-by-step process evaluates `0`

, then `2`

, and finally `6`

. In this particular case, for this particular element, the correct index is found even if the sequence is not sorted.

Now that you're more familiar with the algorithm and its Python implementation, here we have a more complex example:

We want to find the index of the element **45** in this list using Binary Search:

The lower and upper bounds are selected:

The middle element (**26**) is selected:

But the middle element (**26**) is not the element that we are looking for, it is smaller than **45**:

So we can discard all the elements that are smaller than the middle element and select new bounds. The new lower bound (**27**) is the element located immediately to the right of the previous middle element:

π‘ **Tip:** Remember that the list is already sorted.

The new middle element (**30**) is selected:

The middle element (**30**) is not the element that we are looking for, it is smaller than **45**:

We can discard the elements that are smaller than or equal to **30** that have not been discarded already. The lower bound is updated to **32**:

Here we have an interesting case: the middle element is one of the bounds of the current interval because `(7+8)//2`

is `7`

.

The middle element (**32**) is not the element that we are looking for (**45**), it is smaller.

We can discard the elements that are smaller than or equal to **32** that have not been discarded already.

Here we have another very interesting case: the interval only has one element.

π‘ **Tip:** This interval is valid because we wrote this condition `while high <= low:`

, which includes intervals where the index of the lower bound is equal to the index of the upper bound.

The middle element is the only element in the interval because `(8+8)//2`

is `8`

, so the index of the middle element is **8** and the middle element is **45**.

Now the middle element is the element that we are looking for, **45**:

So the value **8** (the index) is returned:

```
>>> binary_search([1, 3, 7, 15, 26, 27, 30, 32, 45], 45)
8
```

πΉ Extra Practice
If you would like to have some extra practice with this algorithm, try to explain how the algorithm works behind the scenes when it's applied to this list to find the integer **90**:

```
[5, 8, 15, 26, 38, 56]
```

- What happens step by step?
- What value is returned?
- Is the element found?

**I really hope you liked my article and found it helpful.** Now you can implement the Binary Search algorithm in Python. Check out online course "Python Searching & Sorting Algorithms: A Practical Approach"

Python tutorial for beginners - Learn Python for Machine Learning and Web Development. Can Python be used for machine learning? Python is widely considered as the preferred language for teaching and learning ML (Machine Learning). Can I use Python for web development? Python can be used to build server-side web applications. Why Python is suitable for machine learning? How Python is used in AI? What language is best for machine learning?

Python tutorial for beginners - Learn Python for Machine Learning and Web DevelopmentTABLE OF CONTENT

- 00:00:00 Introduction
- 00:01:49 Installing Python 3
- 00:06:10 Your First Python Program
- 00:08:11 How Python Code Gets Executed
- 00:11:24 How Long It Takes To Learn Python
- 00:13:03 Variables
- 00:18:21 Receiving Input
- 00:22:16 Python Cheat Sheet
- 00:22:46 Type Conversion
- 00:29:31 Strings
- 00:37:36 Formatted Strings
- 00:40:50 String Methods
- 00:48:33 Arithmetic Operations
- 00:51:33 Operator Precedence
- 00:55:04 Math Functions
- 00:58:17 If Statements
- 01:06:32 Logical Operators
- 01:11:25 Comparison Operators
- 01:16:17 Weight Converter Program
- 01:20:43 While Loops
- 01:24:07 Building a Guessing Game
- 01:30:51 Building the Car Game
- 01:41:48 For Loops
- 01:47:46 Nested Loops
- 01:55:50 Lists
- 02:01:45 2D Lists
- 02:05:11 My Complete Python Course
- 02:06:00 List Methods
- 02:13:25 Tuples
- 02:15:34 Unpacking
- 02:18:21 Dictionaries
- 02:26:21 Emoji Converter
- 02:30:31 Functions
- 02:35:21 Parameters
- 02:39:24 Keyword Arguments
- 02:44:45 Return Statement
- 02:48:55 Creating a Reusable Function
- 02:53:42 Exceptions
- 02:59:14 Comments
- 03:01:46 Classes
- 03:07:46 Constructors
- 03:14:41 Inheritance
- 03:19:33 Modules
- 03:30:12 Packages
- 03:36:22 Generating Random Values
- 03:44:37 Working with Directories
- 03:50:47 Pypi and Pip
- 03:55:34 Project 1: Automation with Python
- 04:10:22 Project 2: Machine Learning with Python
- 04:58:37 Project 3: Building a Website with Django

**Thanks for reading** β€

If you liked this post, share it with all of your programming buddies!

Follow us on **Facebook** | **Twitter**

β Complete Python Bootcamp: Go from zero to hero in Python 3

β Machine Learning A-Zβ’: Hands-On Python & R In Data Science

β Python and Django Full Stack Web Developer Bootcamp

β Complete Python Masterclass

β Python Programming Tutorial | Full Python Course for Beginners 2019 π

β Top 10 Python Frameworks for Web Development In 2019

β Python for Financial Analysis and Algorithmic Trading

β Building A Concurrent Web Scraper With Python and Selenium

In this "Python Tutorial: Data Science vs. Web Development" to provide a comparison on the two completely different purposes of using Python language and help understand that it is not necessary to know Python as a web programming language for doing data science in Python.

Python programming has various frameworks and features to expand in web application development, graphical user interfaces, data analysis, data visualization, etc. Python programming language might not be an ideal choice for web application development, but is extensively used by many organizations for evaluating large datasets, for data visualization, for running data analysis or prototyping. Python programming language is gaining traction amongst users for data science whilst being outmoded as a web programming language. The idea of this blog post is to provide a comparison on the two completely different purposes of using Python language and help understand that it is not necessary to know Python as a web programming language for doing data science in Python.

Organizations of all sizes and industries β from the top financial institutions to the smallest big data start-ups are using Python programming language to run their business.

Python language is among the popular data science programming languages not only with the top big data companies but also with the tech start up crowd. Python language ranks among the top 10 programming languages to learn in 2019.

Python language comes in the former category and is finding increased adoption in numerical computations, machine learning and several data science applications. Python language can do anything, excluding performance dependent and low level stuff. The best bet to use Python programming language is for data analysis and statistical computations. Learning Python programming for web development requires programmers to master various web frameworks like Django that can help the build websites whereas learning Python for data science requires data scientists to learn the usage of regular expressions, get working with the scientific libraries and master the data visualization concepts. With completely different purposes, programmers or professionals who are not knowledgeable about web programming concepts with Python language can easily go ahead and pursue data science in Python programming language without any difficulty.

Python is a 23-year-old powerful expressive dynamic programming language where a programmer can write the code once and execute it without using a separate compiler for the purpose. Python in web development supports various programming paradigms such as structured programming, functional programming and object oriented programming. Python language code can be easily embedded into various existing web application that require a programming interface. However, Python language is a preeminent choice for academic, research and scientific applications which need faster execution and precise mathematical calculations.

Python web programming requires programmers to learn about the various python web development frameworks, which can be intimidating because the documentation available for the python web development frameworks might be somewhat difficult to understand. However, it is undeniable that to develop a dynamic website or a web application using Python language, learning a web framework is essential.

There are several Python web application frameworks available for free like-

**Django**

Django is the python web development framework for perfectionists with deadlines. Python web development with django is best suited for developing database driven web applications with attractive features like automatic admin interface and a templating system. For web development projects that donβt require extensive features, Django may be an overkill because of its confusing file system and strict directory structure. Some companies that are using python web development with django are The New York Times, Instagram, and Pinterest.

**Flask**

It is a simple and lightweight solution for beginners who want to get started with developing single-page web applications. This framework does not support for validation, data abstraction layer and many other components that various other frameworks include. It is not a full stack framework and is used only in the development of small websites.

**CherryPy**

It emphasizes on Pythonic conventions so that programmers can build web applications just the way they would do it using object oriented Python programming. CherryPy is the base template for other popular full stack frameworks like TurboBears and Web2py.

There are so many other web frameworks like Pyramid, Bottle, and Pylons etc. but regardless of the fact, whichever web framework a python programmer uses, the challenge is that he/she needs to pay close attention to detailing on the tutorials and documentation.

Python programming language probably is an impractical choice for being chosen as a web programming language β

Python for web development requires non-standard and expensive hosting particularly when programmers use popular python web frameworks for building websites. With PHP language being so expedient for web programming, most of the users are not interested in investing in Python programming language for web development.

Python language for web development is not a commonly demanded skill unlike demand for other web development languages like PHP, Java or Ruby on Rails. Python for Data science is gaining traction and is the most sought after skill companies are looking for in data scientists, with its increased adoption in machine learning and various other data science applications.

Python for web development has come a long way but it does not have a steep learning curve as compared to other web programming languages like PHP.

Why Python for Data Science is the best fit?

Python programming is the core technology that powers big data, finance, statistics and number crunching with English like syntax. The recent growth of the rich Python data science ecosystem with multiple packages for Machine learning, natural language processing, data visualization, data exploration, data analysis and data mining is resulting in Pythonification of the data science community. Today, Python data science language has all the nuts and bolts for cleaning, transforming, processing and crunching big data. Python is the most in-demand skill for data scientist job role. A data scientist with python programming skills in New York earns an average salary of $180,000

Data Scientists like to work in a programming environment that can quickly prototype by helping them jot down their ideas and models easily. They like to get their stuff done by analysing huge datasets to draw conclusions. Python programming is the most versatile and capable all-rounder for data science applications as it helps data scientists do all this productively by taking optimal minimal time for coding, debugging, executing and getting the results.

The real value of a great enterprise data scientist is to use various data visualizations that can help communicate the data patterns and predictions to various stakeholders of the business effectively, otherwise it is just a zero-sum game. Python has almost every aspect of scientific computing with high computational intensity which makes it a supreme choice for programming across different data science applications, as programmers can do all the development and analysis in one language. Python for data science links between various units of a business and provides a direct medium for data sharing and processing language.

- Python has a unified design philosophy that focuses on ease of use, readability and easy learning curve for data science.
- Python has high scalability and is much faster when compared to other languages like Stata, Matlab.
- There are more and more data visualization libraries and cool application programming interfaces being added for inclusion of graphics to depict the results of data analysis.
- Python has a large community with good number of data science or data analytics libraries like Sci-Kit learn, NumPy, Pandas, and Statsmodels, SciPy etc. which have rich functionality and have been tested extensively. Data analysis libraries in Python language are growing over time.

Data analysis and Python programming language go hand in hand. If you have taken a decision to learn Data Science in Python language, then the next question in your mind would be βWhat are the best data science in Python libraries that do most of the data analysis task? Here are top data analysis libraries in Python used by enterprise data scientists across the world-

**NumPy**

It is the foundation base for the higher level tools built in Python programming language. This library cannot be used for high level data analysis but in-depth understanding of array oriented computing in NumPy helps data scientists use the Pandas library effectively.

**SciPy**

SciPy is used for technical and scientific computing with various modules for integration, special functions, image processing, interpolation, linear algebra, optimizations, ODE solvers and various other tasks. This library is used to work with NumPy arrays with various efficient numerical routines.

**Pandas**

This is the best library for doing data munging as this library makes it easier to handle missing data, supports automatic data alignment, supports working with differently indexed data gathered from multiple data sources.

**SciKit**

This is a popular machine learning library with various regression, classification and clustering algorithms with support for gradient boosting, vector machines, naΓ―ve Bayes, and logistic regression. This library is designed to interoperate with NumPy and SciPy.

**Matplotlib**

It is a 2D plotting library with interactive features for zooming and panning for publication quality figures in different hard copy formats and in interactive environments across various platforms.

Complete hands-on Machine Learning tutorial with Data Science, Tensorflow, Artificial Intelligence, and Neural Networks. Introducing Tensorflow, Using Tensorflow, Introducing Keras, Using Keras, Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), Learning Deep Learning, Machine Learning with Neural Networks, Deep Learning Tutorial with Python

Machine Learning, Data Science and Deep Learning with PythonExplore the full course on Udemy (special discount included in the link): http://learnstartup.net/p/BkS5nEmZg

In less than 3 hours, you can understand the theory behind modern artificial intelligence, and apply it with several hands-on examples. This is machine learning on steroids! Find out why everyoneβs so excited about it and how it really works β and what modern AI can and cannot really do.

In this course, we will cover:

β’ Deep Learning Pre-requistes (gradient descent, autodiff, softmax)

β’ The History of Artificial Neural Networks

β’ Deep Learning in the Tensorflow Playground

β’ Deep Learning Details

β’ Introducing Tensorflow

β’ Using Tensorflow

β’ Introducing Keras

β’ Using Keras to Predict Political Parties

β’ Convolutional Neural Networks (CNNs)

β’ Using CNNs for Handwriting Recognition

β’ Recurrent Neural Networks (RNNs)

β’ Using a RNN for Sentiment Analysis

β’ The Ethics of Deep Learning

β’ Learning More about Deep Learning

At the end, you will have a final challenge to create your own deep learning / machine learning system to predict whether real mammogram results are benign or malignant, using your own artificial neural network you have learned to code from scratch with Python.

Separate the reality of modern AI from the hype β by learning about deep learning, well, deeply. You will need some familiarity with Python and linear algebra to follow along, but if you have that experience, you will find that neural networks are not as complicated as they sound. And how they actually work is quite elegant!

This is hands-on tutorial with real code you can download, study, and run yourself.