Introduction
Setup
Operators
variable
String
Number
Dtype & Comment
Lists
Tuples
Sets
Dictionaries
if else statement
For Loop
While Loop
iterator & iterable
Pre-Built functions
Function
Decorators
generators
Types of errors
Exceptions Handling
OOP
Regular expression
Create module
OS module
Date module
Random module
Math
Statistics module
Exercise 1
Exercise 2
Exercise 3
Learn Data Structure & Algorithm
Learn Numpy
Learn Pandas
Learn Matplotlib
Learn Seaborn
Learn Statistics
Learn Math
Learn MATLAB
Learn Machine learning
Learn Github
Learn OpenCV
Learn Deep Learning
Learn MySQL
Learn MongoDB
Learn Web scraping
Learn Excel
Learn Power BI
Learn Tableau
Learn Docker
Learn Hadoop
We use a while loop to execute a set of statements as long as a condition is true.
Example: 1
In example 1, we can see a variable i which value is 1. Now we want to print "i" value as long as "i" is less
than 6. So here condition is i is less than 6. So the loop will start printing i and will stop when the value
of i is greater than 6. But here is a problem and that is the value of i is 1 so there is no chance to have a
value of i is 2, 3, 4 so on.
That's why i=i+1 was used. It will increase the i value after the loop prints the value of i.
This way we can increase the value of i. Here we increase the value by 1 but if we want to increase the value
by 2 then write i=i+2.
If we want more then write that number means i=i+n. Write any number in the position of n.
When the loop runs for the first time it will check that is the value of i is greater than 6 or not, if not
then it will go inside of the loop. Inside the loop, the first work is to print the value of i. Then the
second work is to increment the value of i. Now, this incremented value will become the value of i variable.
So now i variable value is 2.
Then again the condition will check and if true then again i value will print and again i value will increase
then again variable i will have a new value and then again the condition will be checked.
So this process will happen again and again until the condition become false. So this way we got the output 1
2 3 4 5
In example 2, we just changed the condition and the condition is the value of i must be greater than 1 and less than 6. The working process is the same. Here the increment of i values is 2.
Example:3While using the while loop if we want to stop the loop at a particular point then we use the break statement. Pass a condition in the while loop and if the condition get matched then the loop will break.
Example:While using the while loop if we want that the loop will do its work normally but when it gets a particular value it will ignore it then we use continue statement. Suppose we want to print numbers 1-5 but don't want to print 3 then we will give condition if variable==3 then continue.
Example:By else statement, we can run a block of code one time when the condition becomes false or not true.
Example:To run an infinite loop, in the condition we have to write while true. If we write like this, then the code written inside the loop will execute/run/work infinite time.