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

Python while loop

What is python while loop?

We use a while loop to execute a set of statements as long as a condition is true.

Example: 1
Input
i=1
while i< 6:
   print(i)
   i=i+1 Output
1
2
3
4
5

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

Example:2
Input
i=2
while 1< i< 6:
   print(i)
   i=i+2
Output
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:3
Input
i=2
while 1< i< 6:
   print("love this website",i)
   i=i+1
Output
Love this website 2
Love this website 3
Love this website 4
Love this website 5
Example:4
Input
while 1< i and i< 6:
   print("love this website",i)
   i=i+1
Output
Love this website 2
Love this website 3
Love this website 4
Love this website 5
Example:5
Input
i=2
while 1< i or i< 6:
   print("love SelfTaught",i)
   i=i+1
Output
Love SelfTaught 2
Love SelfTaught 3
Love SelfTaught 4
Love SelfTaught 5
Example:6
Input
i=1
while i<= 6:
   print(f"hello world {i}")
   i=i+1
Output
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
Example:7
Input
Name=input( "Enter your Name:")
i=0
while i< len(Name):
   print(f"{Name[i]}:{Name.count(Name[i])}")
   i=i+1
Output
Enter your name:Rafsun
R:1
a:1
f:1
s:1
u:1
n:1
Example:8
Input
number=int(input("Enter Number:"))
sum=0
i=1
while 1<= number:
  sum=sum+i
  i=i+2
  print("sum")
Output
What will be the output?

break statement

While 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:
Input
i=1
while i< 6:
  print(i)
  if i==3:
     break
     i=i+1
Output
1
2

continue statement

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:
Input
i=1
while i<6:
  print(i)
  if i==3:
     continue
     i=i+1
Output
1
2
4
5

else statement

By else statement, we can run a block of code one time when the condition becomes false or not true.

Example:
Input
i=1
while i<6:
   print(i)
   i=i+1
else:
  print("Now i become greater than 6. So sorry can't print more") Output
1
2
3
4
5
Now i become greater than 6. So sorry can't print more

How to run infinite loop?

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.

Input
while True:
  print("RafsanAhmad")
Output
RafsanAhmad
RafsanAhmad
RafsanAhmad
RafsanAhmad
RafsanAhmad
RafsanAhmad
RafsanAhmad
RafsanAhmad
RafsanAhmad
RafsanAhmad..................

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.