10 Best Ways To Use While Loop In Python

Share This Post

Let’s look at how the while statement in Python is used to create loops. We’ll start with the basics and add to it as we go. The following is the format of a basic while loop: and here you will know 10 best ways to use while loops in python

10 Best Ways To Use While Loop In Python


contains the content of the loop, which is the block that will be run continuously. Indentation is used to indicate this, just like in an if expression.

The else Clause

A while loop in Python can have an optional else clause at the end. This is a feature that Python has that most other programming languages lack. The following is the syntax:



When the while loop comes to an end, the else clause will be executed. “How is that useful?” you might be thinking right now. You might achieve the same result without the else clause by placing those statements immediately after the while loop:


Infinite Loops

Assume you’ve written a while loop that, in theory, will never finish. Sounds weird, right?

Consider this example:

while True:


Traceback (most recent call last):

File “”, line 2, in


This code was halted by pressing Ctrl+C, which causes a keyboard interrupt. It would have gone on indefinitely if it hadn’t been stopped. In the output shown, many foo output lines have been eliminated and replaced by a vertical ellipsis.

Nested while loops

Python control structures can be layered within one other in general. If/elif/else conditional statements, for example, can be nested:

if age == 18:
    if gender == 'M':
    if gender == 'M':
    if gender == 'M':

A while loop can also be placed within every while loop, see above:

a = ['foo', 'bar']

while len(a):

b = ['baz', 'qux']
while len(b):
    print('', b.pop(0))

Within nested loops, a break or continue statement applies to the nearest enclosing loop:

One-Line while Loops

A while loop, like an if statement, can be specified on a single line. Semicolons (;) can be used to separate multiple statements in the block that makes up the loop body:

n = 5






However, this only works with simple statements. Two compound statements cannot be combined into a single line. As a result, you can declare a while loop on one line and write an if statement on the same line:

if True:

For Loop

Iterators are what Python calls “for loops.”

“For Loop,” like the while loop, is used to repeat the program.

However, unlike the while loop, which is conditional on true or false, the for loop is not conditional. The iteration of elements in a “For Loop” is determined by the components to be iterated.


#Example file for working with loops


#define a while loop


<h3>Break Statements in For Loop</h3>
A breakpoint is a one-of-a-kind function in the For Loop that allows you to pause or stop the execution of the loop.


<strong>#use a for loop over a collection</strong>

Months = ["Jan","Feb","Mar","April","May","June"]

for m in Months:

# use the break and continue statements

for x in range (10,20):
    if (x == 15): break
    # if (x % 2 == 0) : continue

Continue Statement Loop

The continue function, as its name implies, will end the current iteration of the for loop but continue the remaining iterations.


#use a for loop over a collection

Months = ["Jan","Feb","Mar","April","May","June"]

for m in Months:
    print (m)

# use the break and continue statements

for x in range (10,20):
    if (x == 15):

    if (x % 5 == 0) :

Enumerate Loop

Enumerate() is a Python built-in system that offers every element of an iterable collection an index. It constructs an iterable object loop and returns the object in enumerable form while keeping track of the current item. The list() method can be used in a for loop to transform this object into a list.


If we want to number our months (Jan, Feb, Marc,….June), we declare the variable I to enumerate the numbers, and the variable m to print the month number in a list.

#use a for loop over a collection

Months = ["Jan","Feb","Mar","April","May","June"]

for I, m in enumerate (Months):

# use the break and continue statements

for x in range (10,20):
    if (x == 15): break
    if (x % 5 == 0) : continue
    print (x)

Read More: 10 Ways to Use Python For Loop, What is For Loop in Python?


Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img