How to define WHILE Loop in Python?

This recipe helps you define WHILE Loop in Python

Recipe Objective

In python, sometimes we want that same function to be iterated on many values. So for this passing every value from the function may be a hectic task. We can do this by defining a loop

So this is the recipe on how we can define WHILE Loop in Python.

Step 1 - Defining Loop

sales = 7 while sales > 0: print("sales is greater than zero") sales = sales - 1

Here we have defined While loop which iterate the functions passed inside it till the condition inside the while loop is true. Here we have given the condition that till sales is greater than zero it will iterate and inside the loop we have passed the function to decrease the sales by 1 in each step. So the output comes as

sales is greater than zero
sales is greater than zero
sales is greater than zero
sales is greater than zero
sales is greater than zero
sales is greater than zero
sales is greater than zero

Download Materials

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.