Python Tutorial
About Lesson

In this Python Loop lesson we are going to learn Working with For & While Loops in Python, 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

A for loop is used for iterating over a (a list, a tuple, a dictionary, or a string).

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.

 

 

 

OK 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

 

 

 

OK 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

 

 

Python While Loop

With the while loop we can execute a set of statements as long as a certain condition is true.

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.

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found