Python Programming

How to Use break, continue and pass in Python Programming

Learn how, when and why to use break, continue and pass in Python

Suraj Gurav
Towards Data Science
6 min readMar 21, 2022

--

https://pixabay.com/photos/bulletin-board-stickies-post-it-3127287/

Python — being flexible — is the best solution to automate and to do repetitive tasks in efficient way!

In Python (and other programming languages also), the concept of loops helps to iterate over a list, tuple, string, dictionary, and a set.

Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression.

This can be achieved using few keywords which can be used to alter the flow or execution of the loops. And in Python those keywords are — break, continue, pass. 💡

And it is important to know when and how to use these keywords to control the flow of your loop correctly!

Therefore, in this article, I am discussing (with examples) when-why-how of break, continue and pass in Python.

The concept behind these keywords is same irrespective of the programming language!

Let’s get started!

break statement in Python

--

--

Towards Data Science
Towards Data Science

Published in Towards Data Science

Your home for data science and AI. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.

Suraj Gurav
Suraj Gurav

Written by Suraj Gurav

Analytics professional and writer. I write about Data Science, Python, SQL & interviews. Join Medium today to get all my articles: https://tinyurl.com/3fehn8pw

Responses (3)

What are your thoughts?