Python Loops: for and while
In Python, there are mainly two types of Loops, for loop and while loop. Both these loops are mainly used to execute a set of statements more than one time. For example, if we want to print “Happy Coding” for 100 times then we have to write the print statement for 100 times, which makes our code longer and it breaks the DRY (Don’t Repeat Yourself) rule of Programming.
So to counter with this situation we will use Loops. With the help of loops, we can do the same task in just few lines of code. Here is an example:
Example
for i in range(100): print("Happyt Coding!")
Run this code on any python compiler and you will see that it prints “Happy Coding!” 100 times.
So with the help of above example you have got the little bit idea about loops and why we use them. But you may wonder when to use for loop and when to use while loop, so keep reading this article and you get your answer.
Python: for Loop
Python for loop is used to iterate over a sequence of items, like list, tuple, string etc. The for loop executes a block of code for each item in the sequence until all elements have been processed. Here is an example:
Example
items = ["Table", "Book", "Fan", "Pen", "laptop"] for x in items: #Here x is actually a variable print(x)
Outputs
Table Book Fan Pen laptop
Example Explained
In an above example we have created a list namely items, where we store some elements (Table, Book…). Then we use the for loop to iterate through the list. In each iteration, the current element is assigned to the variable x. So, x will take the value of each item in the list one by one as the loop progresses. At last, we use the print statement, that prints the value of the variable x
. Since x
represents the current item in the items
list, it will print each item one by one during each iteration of the loop.
Iterating through a String
We can also use the for loop to iterate through a string. Here is an example:
Example
text = "Happy Coding!" for i in text: print(i)
Outputs
H a p p y C o d i n g !
Take a look at the below diagram to clear your doubts about for loop.

Python: while Loop
In Python, the while
loop is used to repeatedly execute a block of code as long as a certain condition is True. It will stop running when the condition becomes False. Here is an example:
Example
x = 5 while x > 0: print(x) x -= 1
Outputs
5 4 3 2 1
Example Explained
In an above code we first initialize a variable x
with the value 5, and use this variable x
in a loop. The while loop
repeatedly executes the code block indented below it as long as the condition x > 0
is True
. In each iteration, it checks whether x
is greater than 0, and if so, it continues to execute the loop. The 3rd line of code, print(x)
prints the value of the variable x
. During each iteration of the loop, it will print the current value of x
. The last line of code, x-=1
decrements the value of x
by 1 in each iteration. It’s equivalent to x = x - 1
.
Checkout the below diagram to clear your doubts about Python while loop.

Difference Between Python for and while Loops
S.NO. | for Loops | while Loops |
---|---|---|
1. | A for loop is used to iterate over a sequence of characters or any iterable object. | A while loop is repeatedly used to execute a block of code as long as a certain condition is True. |
2. | It executes a block of code for each item in the sequence until all elements have been processed. | It will keep running until the condition becomes False. |
3. | The loop variable (often referred to as the iterator) takes the value of each item in the sequence during each iteration. | Here the condition is evaluated before each iteration, and if it is True, the loop body is executed. Otherwise, the loop is exited. |
4. | for loops are typically used when the number of iterations is known or when you want to iterate over a specific range or collection of items. | while loops are commonly used when the number of iterations is not known, and you want to continue executing the loop until a specific condition is met. |