How to use CONTINUE and BREAK statement within a loop in Python?

How to use CONTINUE and BREAK statement within a loop in Python?

How to use CONTINUE and BREAK statement within a loop in Python?

This recipe helps you use CONTINUE and BREAK statement within a loop in Python


Recipe Objective

Have you tried to stop a loop or continue a loop with or without a condition.

So this is the recipe on how we can use CONTINUE and BREAK statement within a loop in Python.

Step 1 - Importing Library

import random

We have only imported numpy which is needed.

Step 2 - Creating a loop

We have created a while loop after creating an object. We have made conditions for keep the loop working and to stop the loop. We can stop the loop by using break function. running = True while running: s = random.randint(0,5) if s < 3: print(s, ": It is too small, starting again.") continue if s == 4: running = False print("It is 4! Changing running to false") if s == 5: print("It is 5! Breaking Loop!") break We get the output as:

0 : It is too small, starting again.
2 : It is too small, starting again.
It is 5! Breaking Loop!

Relevant Projects

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.