For loop vs while loop in Python

In this Python tutorial, we will discuss the For loop vs while loop in Python. Here we will also cover the below example:

  • What are loops
  • Loops in python
  • For loop in python
  • While loop in python
  • For loop vs while loop in Python

What are loops?

  • Loops basically allow us to terminate a statement or a group multiple times in a given condition.
  • In Python, there are many conditions defined in the beginning to enter the loop.
  • In Python, if the condition becomes false the loop stops and the condition will exit from the loop.

Read: Python While loop condition

Loops in Python

  • Now we will discuss how many types of loops are available in Python.
  • There are three types of loops one is for loop, while loop, and nested loop. So in this tutorial, we will discuss only for loop and while loop concept.

While loop

In Python, while loop is basically used to know how many iterations are required.

Here is the Flow chart of while-loop

Flow chart while loop
Flow chart while loop

Syntax of while loop:

While condition:
    statement

In Python, you don’t know how many times you need to execute the statements that are present inside your body loop.

Let us check the while loop condition with the help of an example:

Source Code:

z = 7

while z < 12:
    print(z)
    z += 1

In the above code, we write this while loop condition z is less than 12 (x<12). The loop completes four ways and it stops when z is equal to 12.

Here is the execution of the following given code

While loop in Python
While loop in Python

Read: Python While loop condition

For loop in Python

In Python, the for loop is used when the user knows the number of iterations that are required. The user knows how many times the statement that is available inside the loops needs to be terminated.

Here you can check the Flow-chart of the For-loop

Flow chart For loop in python
Flow chart For loop in python

Here is the Syntax of For loop

for value in range:
 statement

In Python, for-loop provides you a syntax that will contain three information So first will be the condition after that the initial value and then incrementation of the given value.

Here we can check them for loop condition with the help of an example

Source Code:

Country_name = ['Estonia', 'France', 'Iceland']

for new_val in Country_name:
  print(new_val)

We have written a for loop condition in the above code to display the country names in Output. In this example first, we initialize a list ‘country_name’ which contains three countries.

Here is the Implementation of the following given code

For loop in Python
For loop in Python

Read: Python For Loop with Examples

For loop vs while loop in Python

Different basis
for comparison
For loopWhile loop
Declarationfor value in range:
<statement>
while condition:
<statement>
Use loopThe for loop is used only
when we already know the number of iteration.
The while loop is used when we did not know how many iterations are required.
ConditionIf there is no condition then the loop iterates infinite time.But in the case of while if the condition is missing then it shows an error.
IterationIn the loop statement, it is always written at the top.But in the case of while we can store statements anywhere in the loop body.
Working processThe control comes to the condition and it will check the items from the given sequence it will terminate the statement and start the process again and then from the given range it will select up the next value and execute the statement and it will keep terminating until the range becomes over.In the case of while the control will check within the loop only when this while condition is true when the condition is true, it will terminate the body of the loop and then again check the condition whether it is true or not
InitializationIn the case of for loop if the condition is not true the first time then control will never enter in a loop.In the case of a while loop if the condition is true then the loop is iterated.
Examples:alpha = [‘a’,’b’,’c’]
for x in alpha:
print(x)
n = 2

while n < 6:
print(n)
n += 1
For loop vs while loop

You may also like reading the following articles.

In this Python tutorial, we have discussed the For loop vs while loop in Python. Here we have also covered the following topics:

  • What are loops
  • Loops in python
  • For loop in python
  • While loop in python
  • Difference between for loop and while loop in Python