Python Tutorial
About Lesson

In this Python Tutorial we are going to learn about Python For & While Loop, so we will learn how you can create for and while loops in Python, first we want to learn about for loop.

 

 

Python For Loop

Python For Loop is used for iterating over a (a list, a tuple, a dictionary, or a string). or we can say in Python for loop is a control flow statement that allows you to iterate over a sequence of elements, such as a list, tuple, or string and perform set of operations on each element in the sequence.

 

The general syntax of a for loop in Python is:

Here variable is new variable that takes on the value of each element in the sequence one by one and the indented block of code under for statement is executed for each iteration of the loop.

 

OK first we want to loop though a list, we are going to create a Python List and after that we want to iterate on the list.

 

 

 

This is the result.

Python Loop - Working with For & While Loops in Python
Python Loop – Working with For & While Loops in Python

 

 

 

Also you can do looping on a String.

 

 

 

Now let’s iterate over Python Dictionary and find key and value.

 

 

 

This is the result.

Python Iterating On Dictionary
Python Iterating On Dictionary

 

 

 

Also you can loop through all dictionary items.

 

 

 

This is the result.

Loop Through Dictionary Items
Loop Through Dictionary Items

 

 

 

Now let’s loop through values in Dictionary.

 

 

 

This is the result.

Loop Through Values In Dictionary
Loop Through Values In Dictionary

 

 

 

Let’s iterate over a tuple.

 

 

 

This is the result.

Python Iterating On Tuples
Python Iterating On Tuples

You can use for loop to perform any kind of operation on sequence of elements such as searching for specific value, counting the number of elements that meet certain condition or modifying the elements in the sequence. by understanding how to use for loops effectively you can simplify your code and perform complex operations on data with easy.

 

 

Python While Loop

With the while loop we can execute a set of statements as long as a certain condition is true, or we can say that in Python a while loop is control flow statement that allows you to repeatedly execute a block of code while a certain condition is true. while loop continues to execute the block of code as long as the condition remains true.

 

The general syntax of while loop in Python is:

Here condition is a boolean expression that is evaluated before each iteration of the loop. If the condition is True, the block of code under the while  statement is executed. After each iteration, the condition is checked again, and if it remains True the loop continues to execute.

 

 

This is a basic example

First we have created a number variable, and after that we are using while, now when the condition is true we are printing our number and incrementing the number,  when the condition became false we don’t print the number.

 

 

 

This is the result.

Python While Loop
Python While Loop

 

 

 

This is another example.

 

 

 

This is the result.

Python While Loop Example
Python While Loop Example

 

 

 

With the break statement we can stop the loop even if the while condition is true.

 

 

With the continue statement we can stop the current iteration, and continue with the next.

 

You can use  while loop to perform any kind of operation that requires repeated execution while certain condition is met such as searching for specific value or processing a stream of data. However you should be careful to ensure that the loop condition will eventually become False to avoid an infinite loop that will run forever.

 

 

Does Python have Do While Loop ?

Python does not have built in do while loop like some other programming languages. In do while loop, the loop body is executed at least once before the loop condition is checked.

However, in Python, you can achieve the same behavior using while loop with break statement to exit the loop. for example:

In this example the loop condition is always True, so the loop will execute code inside the loop body at least once. the loop body can contain any code that needs to be executed, and if statement with the break statement inside can be used to exit the loop when certain condition is met.

 

While Python does not have a built in do while loop but it provides flexible constructs like while loops and conditional statements that can be used to achieve the same behavior in a  way that is compatible with Python’s syntax and design principles.

 

 

Difference between For and While Loops in Python

The main difference between for and while loops in Python is the way they iterate through sequence of elements.

for loop is used to iterate through sequence of elements, such as list, tuple or string and perform set of operations on each element in the sequence. the loop variable takes on the value of each element in the sequence one by one, and the loop body is executed for each element in the sequence.

while loop on the other hand is used to repeatedly execute block of code while certain condition is true. the loop body is executed repeatedly as long as the condition remains true. the condition is typically boolean expression that is evaluated before each iteration of the loop.

In general you should use for loop when you know the number of iterations in advance, such as when iterating through a sequence of known length. for loops are usually more concise and readable than while loops in this case.

You should use while loop when you don’t know the number of iterations or when you need to repeat block of code until certain condition is met. while loops can be more flexible and powerful than for loops in this case, but they can also be more complex and harder to read and debug.

 

 

This is an example that illustrates the difference between for and while loops in Python:

In this example both loops accomplish the same task of printing the elements of list. However for loop is more concise and easier to read, while the while loop is more verbose and requires an additional variable (i) to keep track of the index of the current element in the list.