Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways

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.

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.

We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist 
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert

Article By,
Yashika Khurana
Intern | The Data Monk

About TheDataMonkGrand Master

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 :)

Follow Me

Leave a reply