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
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
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
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
Read: Python For Loop with Examples
For loop vs while loop in Python
Different basis for comparison | For loop | While loop |
---|---|---|
Declaration | for value in range: <statement> | while condition: <statement> |
Use loop | The 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. |
Condition | If 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. |
Iteration | In 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 process | The 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 |
Initialization | In 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 |
You may also like reading the following articles.
- Python while loop multiple conditions
- Python list comprehension using if-else
- Python while loop multiple conditions
- Python for loop index
- Python Absolute Value
- Python loop through a list
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
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.