important questions about the while loop in Python

Python Loop while and for loop with examples for Data Analytics

Python While loop

A block of code is regarded to consist of all the statements that are followed by the same number of character spaces following a programming construct. The way that Python groups statements is by indentation. When a while loop is run, expr is first tested in a boolean context, and if it returns true, the body of the loop is run. The expression is then verified again, and if it is still true, the body is run once again. This process is repeated until the expression is no longer true.

•While loop execute a set a statement as long as test statement is true.
•A relevant variable has to be declared at the beginning of the loop
while loop flow chart python

While loop falls under the category of indefinite iteration. Indefinite iteration means that the number of times the loop is executed isn’t specified explicitly in advance.Statements represent all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements

Example of While Loop

Python While Loop

# Python program to illustrate
# while loop
num = 0
while (num < 3):
num = num + 1
print(“vista academy”)

Output

vista academy
vista academy
vista academy

Single statement while block

count = 0
while (count < 5): count += 1; print(“Vista Academy”)

Output

Vista Academy

Vista Academy

Vista Academy

Vista Academy

Vista Academy

Example of using the break statement in while loops

To terminate a while loop prematurely in Python, use the break command.

n = 1
while n < 5:
print(“Hello”)
n = n+1
if n == 3:
break

output
Hello
Hello

Example of using the continue statement in while loops

n = 1

while n < 5:
if n == 2:
n = n+1
continue
print(“Hello”)
n = n+1
Output
Hello
Hello
Hello

The continue statement in Python may be used to end the current iteration of the while loop and begin the next one.

Using if-elif-else statements inside while loop

z = 0

while z < 3:
if z == 0:
print(“z is”,z)
z += 1
elif z == 1:
print(“z is”,z)
z += 1
else:
print(“z is”,z)
z += 1

output


z is 0
z is 1
z is 3

Adding elements to a list using while loop

List = []
i = 0

while len(List) < 4 :
List.append(i)
i += 1
print(List)

output
[0,1,2,3]

Python while loop to print a number series

n = 10

while n <= 100:
print(n ,end = “,”)
n = n+10

OUTPUT
10.20.30.40.50.60.70.80.90,100

10 steps to start career in data science 5 Data Analytics Projects for Beginners 5 Excel Data Analysis Functions You Need to Know 5 Things in Your Resume from Getting Your First Job in Data Science Best Data Analytics training in Dehradun Why to learn Best Data science Training in Dehradun Categories of SQL command to know for Data Analysis Data Analyst vs. Business Analyst: What’s the Difference? Data Cleaning With Python for data analytics Data Science Case Studies given by Top Data Scientists