We use for loop and while loop in Python for different tasks. This article discusses for loop vs while loop in Python to uncover their similarities and differences.
Syntax of For Loop in Python
We use a for loop in Python to iterate through a container object such as a list, tuple, dictionary, etc. It has the following syntax.
for variable_name in iterable_obj:
#do something with variable
Here, variable_name
is a temporary variable used to iterate through the elements of iterable_obj
. The variable takes all the values in the iterable object one by one. We can then perform operations on the variable as per our requirements.
For example, we can print the squares of all the elements of a list using the for loop as shown below.
myList=[1,2,3,4,5]
print("The list is:",myList)
for element in myList:
square=element**2
print("The square of {} is {}.".format(element,square))
Output:
The list is: [1, 2, 3, 4, 5]
The square of 1 is 1.
The square of 2 is 4.
The square of 3 is 9.
The square of 4 is 16.
The square of 5 is 25.
We can also use a for loop to execute a task a fixed number of times. For this, we use the range() function to create a range object. Then, we iterate through the elements of the range object to perform the desired task as shown in the following example.
for element in range(1,6):
square=element**2
print("The square of {} is {}.".format(element,square))
Output:
The square of 1 is 1.
The square of 2 is 4.
The square of 3 is 9.
The square of 4 is 16.
The square of 5 is 25.
Syntax of While Loop in Python
While loop is used to perform a task in Python until a condition is True. It has the following syntax.
While condition:
#do something
Here, the statements inside the while loop are continuously executed until the condition
becomes False. For example, we can use the while loop to print the square of numbers from 1 to 5 as shown below.
value=1
while value<=5:
square=value**2
print("The square of {} is {}.".format(value,square))
value+=1
Output:
The square of 1 is 1.
The square of 2 is 4.
The square of 3 is 9.
The square of 4 is 16.
The square of 5 is 25.
We can also use a while loop to execute a task a fixed number of times. For this, we will need to use a counter variable in place of the variable “value
” to enforce the condition in the while loop as shown above.
For Loop vs While Loop in Python
Although the for loops and While loops can perform similar operations, there are many differences between the two loop constructs. The following table summarizes the differences between for loop vs while loop in Python.
For Loop | While Loop |
For loop is used in Python to iterate through the elements of an iterable object and execute some statements. | While loop is used to execute statements in Python until a condition remains True. |
We cannot execute for loop based on a condition or until a condition is true. | We can use a while loop to iterate through the elements of an iterable object like a list or tuple by finding their length. |
For loop cannot run for an infinite number of times. | We can run a while loop for an infinite number of times. |
We can use the continue statement to skip the execution of statements in a for loop. | We can use continue statements with while loops too to skip the execution of certain statements. |
We can use the break statement to get out of a for loop. | We can use the break statement to get out of a while loop too. |
Conclusion
In this article, we discussed the syntax of for loop and while loop in Python. We also had a discussion on for loop vs while loop in Python to understand the difference between the two loop constructs. To learn more about Python programming, you can read this article on if vs elif vs else if in Python. You might also like this article on Python finally keyword.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.