While, For and Nested Loop

Sakshi Dhameja  7 min read
21 Jul 2023


Learning how to effectively use loop statements in Python can truly transform the way you code. With properly structured while, for, and nested loops, you’ll be able to stand out as a skilled coder by writing shorter, more efficient programs with fewer bugs. But when it comes to actually using these powerful statements, where do you start? If this is something that’s been puzzling you lately, then you might wanna consider enrolling in a Python beginner course. Apart from that, this article will help enlighten and educate you on the basics of loop statement usage in the Python language!

What is a loop in Python

Loop statement in python programming language are one of the most powerful tools of the language. They allow the developer as well as the user to repeat a set of commands for a specific number of times, or until some condition is true. Loop statement in python also makes it easy to loop through items contained in lists, tuples and dictionaries. Loop statement in python add great structure and flexibility to any python code and it allows the coder to accomplish more with fewer lines, and making simple operations much easier than writing out individual lines for each loop to run

Different types of loops in python

For understanding the loop structures in python, you need to know that there are three types of loops in python, those are:

  1. Python while loop
  2. Python for loop
  3. Python nested loop

Python while loop

A python while loop is a control flow statement used to perform repetitive actions while a given condition remains true. A while loop continues the execution until the condition becomes false or is broken by an internal command. It allows developers to create code that can be repeated indefinitely and provides specific conditions in which the iterative action should take place. Using python while loops are a great way to ensure that any code is bug-free and reliable since only the correct input conditions will keep that particular loop running.


while expression:


count = 0
while (count < 10):
   print 'The count is:', count
   count = count + 1
print "Good bye!"


The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
The count is: 9
Good bye!

Python for loop

Python for loops is a fantastic way to automate repeated tasks and save time. Writing out a for loop program in python language can help the developer cycle through an entire databank or operation within a few minutes and it helps to process each item individually by using the same method. This allows for a much more efficient approach compared to systematically running line-by-line code whether it be to produce data or complete computational operations. The python for loop is exceptionally useful and powerful, improving the efficiency of countless workflow processes.


for iterating_var in sequence:


for letter in 'Python': # First Example
   print 'Current Letter :', letter
fruits = ['guava', 'apple', 'mango']
for fruit in fruits: # Second Example
   print 'Current fruit :', fruit
print "Good bye!"


Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : guava
Current fruit : apple
Current fruit : mango
Good bye!

Python Nested loop

Working with nested loop in Python can quickly become complicated. When it comes to nested loops it needs to understand the correct syntax and how to properly structure the nested loop statement are essential elements of writing effective code. With nested loop in python, anyone can access records through a series of iterations until the desired output is achieved. Each nested loop creates an additional layer around the contents of the preceding loop, making it easier for users to sort out complex data sets. A nested loop of python programming language structure helps the developer, as well as the user, write efficient and secure code without having to manually type out multiple commands.

Syntax for Nested for loop

for iterating_var in sequence:
   for iterating_var in sequence:

Syntax for Nested while loop

while expression:
   while expression:


i = 2
while(i < 100):
   j = 2
   while(j <= (i/j)):
      if not(i%j): break
      j = j + 1
   if (j > i/j) : print i, " is prime"
   i = i + 1
print "Good bye!"


2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
Good bye!

In this article, we have learned about the different types of loop statements in Python. We have also seen how to use them with the help of examples. Whether you're a Python beginner or looking to enhance your skills, mastering these loop statements can greatly improve your coding abilities. So why not consider pursuing a Python certification to solidify your knowledge and showcase your expertise? Happy Learning!

About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like Java, Python, C, C++ etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like JavaScript and Cloud.

Accept cookies & close this