Break, Continue and Pass in Python | Python for Data Science | Day 7

For this tutorial, we will walk you through a very important concept – Break, Continue and Pass in Python. This article will primarily focus on the use of the three keywords Break, Continue and Pass in Python by taking instances of different situations and loops.
Break, Continue and Pass in Python

So, without further ado, let’s get started.

Break, Continue and Pass in Python

Let’s study each of the following statements by considering corresponding examples.

  • Break statement

The break statement is used to leave the current loop of execution and the control flow is diverted to the statements that exist outside the current loop (if any). If this statement looks overwhelming, consider the code below to comprehend.

Here, we have used a for loop within the range 1 to 10. If the specified condition i.e. i=5 is met, we wish to break the loop.

As expected, 1, 2, 3 and 4 are printed. But as soon as the if-condition i=5 is met, the loop breaks. Since there are no statements after this loop, the control flow of the execution is not passed further and is terminated, marking the end of the program.

We have taken flowcharts from www.geeksforgeeks.org to facilitate explanation.

Break, Continue and Pass in Python
  • Continue statement

Unlike break statements where we exit the current loop if a certain condition is met, the continue statement is like a pause button. It helps you ignore execution if a certain condition is met, but resume the current loop when the condition is not met. So, instead of ending the loop, we just jump to the next iteration. Wait, it might appear involving at the moment. But just look at the flowchart below and then look at our code to understand what’s really happening.

Let’s look at the code now:

In the loop above, we have specified an if-condition. We wish to print all numbers in range 1 to 10 except 5. So, what does the continue statement do? It pauses the execution at 5 and prints the rest of the numbers.

  • Pass statement

Alright, so we have reached the pass statement. It is the easiest to apprehend. Remember one thing- whenever you want the code to do nothing, just use the pass statement.

See the code below:

Now compare it with the code below:

Do you observe any difference in the outputs? 

No.

Then, what did the pass statement do? We go back to the main point, it did “nothing”.

But, wait! The makers of the language are not this foolish to invent a statement that has no use. It is certainly useful in specific problem such as the one shown below:

Time for a case study:

We work at Seattle Weather Dept. For our users, we need to give “advice of the day” according to the weather.  So, if the weather is sunny, we need to tell them to wear sunscreen. If it’s cold, we tell them to wear sweaters, if it’s rainy, it’s advisable to carry an umbrella. But, if the weather is anything else, no advice, at all!

So, we write the code below:

Great! The system gives valid advice. Let;s try something else. 

Bravo! If the weather is pleasant, absolutely no advice!

This has been made possible with the help of our “do-nothing specialist” . The pass statement.

So, we end our tutorial on this note. But before we go, we have some questions to test your knowledge. We have provided solutions for them but we highly recommend that you refrain from referring them unless you give them a try on your own.

The Data Monk Interview Books – Don’t Miss

Now we are also available on our website where you can directly download the PDF of the topic you are interested in. At Amazon, each book costs ~299, on our website we have put it at a 60-80% discount. There are ~4000 solved interview questions prepared for you.

10 e-book bundle with 1400 interview questions spread across SQL, Python, Statistics, Case Studies, and Machine Learning Algorithms – Ideal for 0-3 years experienced candidates

23 E-book with ~2000 interview questions spread across AWS, SQL, Python, 10+ ML algorithms, MS Excel, and Case Studies – Complete Package for someone between 0 to 8 years of experience (The above 10 e-book bundle has a completely different set of e-books)

12 E-books for 12 Machine Learning algorithms with 1000+ interview questions – For those candidates who want to include any Machine Learning Algorithm in their resume and to learn/revise the important concepts. These 12 e-books are a part of the 23 e-book package

Individual 50+ e-books on separate topics

Important Resources to crack interviews (Mostly Free)

There are a few things which might be very useful for your preparation

The Data Monk Youtube channel – Here you will get only those videos that are asked in interviews for Data Analysts, Data Scientists, Machine Learning Engineers, Business Intelligence Engineers, Analytics Manager, etc.
Go through the watchlist which makes you uncomfortable:-

All the list of 200 videos
Complete Python Playlist for Data Science
Company-wise Data Science Interview Questions – Must Watch
All important Machine Learning Algorithm with code in Python
Complete Python Numpy Playlist
Complete Python Pandas Playlist
SQL Complete Playlist
Case Study and Guesstimates Complete Playlist
Complete Playlist of Statistics

Author: TheDataMonk

I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience 3+ years at Mu Sigma 2 years at OYO 1 year and counting at The Data Monk I am an active trader and a logically sarcastic idiot :)