How to use CONTINUE and BREAK statement within a loop in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

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

0

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.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.