For vs. while loop in Python

Python is a high-level programming language known for its simplicity and readable code structure. With its extensive built-in libraries and functions, Python is used in various applications, from web development and data analytics to artificial intelligence and scientific computing.

Python provides loops to execute specific lines of code more than one time. In this answer, we will explore the differences between for and while loops.

The for loop

A for loop is used to iterate over a sequence of items for a predefined number of times. The iterable sequence can be a list, string, or a range. The syntax of for loop is given below:

for var in <iterable-sequence) :
# code block

In the above syntax, we can see that for loop takes <iterable-sequence> and var.

  • <iterable-sequence> is the collection or sequence of items (list, string, or range) over which the loop iterates.

  • var is a variable that represents each element in the sequence during each iteration.

Let's explore how we can utilize a for loop to iterate through individual elements in multiple iterable sequences:

List iteration

Below is an example explaining how a for loop traverses a Python list. Please click the "Run" button to execute the code:

fruits = ['apple' , 'banana' , 'pineapple' , 'tomato']
for item in fruits:
print(item)

Code explanation

  • Line 1: We create a list of fruits.

  • Line 3: We use the for loop to iterate through each item of the fruits list. The item takes a value from the list on each iteration.

String iteration

Below is an example explaining how a for loop traverses a string. Please click the "Run" button to execute the code:

name = 'Bob'
for char in name:
print(char)

Code explanation

  • Line 1: We create a name variable with a string, Bob.

  • Line 2: We use the for loop to iterate through each item of the name string. On each iteration, the for loop variable char takes a character of the string.

The for loop works until all characters of the name string has not iterated.

Range iteration

for iteration in range (0 , 5):
print(iteration)

Code explanation

  • Line 1: We define the range function that returns a sequence of numbers starting from the first parameter, i.e., 0, to the second parameter, i.e., 5, which is exclusive, meaning it is not included in the generated sequence. The for loop variable iteration takes a number of the range function on each iteration.

The while loop

A while loop is used to execute a block of code until the condition is true. It is used when the number of iterations is not predefined, and we must execute the code repeatedly until the condition is fulfilled. The condition is checked before each iteration. The syntax of the while loop is given below:

while ( condition ) :
# code block

The coding example of a while loop is given below.

Coding example

We create a while loop that checks if the value of i is less than 5, and keeps on printing Hello World until the condition is true.

i = 0
while (i < 5):
print("Hello World " , i)
i+=1

Code explanation

  • Line 2: We create the while loop with a condition i < 5 defined in the parenthesis. The while loop will execute until the condition is true.

  • Line 4: We increment the value of i by 1 on each iteration.

Conclusion

The main difference between for loop and a while loop is that the for loop is used when we have to iterate through a sequence of items, whereas a while loop is used when the code block's execution depends on a condition.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved