Python Loops

Python Loops

In Python Programming, Python Loops are used for iterating a sequence (which is either a list, a dictionary, a tuple, a set, or a string). With Loops in python, we can execute a set of statements, once for each time in a list, dictionary, tuple, set, etc.

In simple words, a loop means repeating a set of instructions for a given number of inputs. So in this tutorial, we will briefly understand all the loops in Python Programming Language with the help of examples which will help you the easiest way to understand.

Types of Loops in Python Programming Language

In Python Programming, there are 3 types of loops that handle looping requirements. All three types of loops are similar in their own way and they all provide similar functions. They are stated below:-

  1. For Loop
  2. While Loop
  3. Nested Loop

We will understand all the loops statements one by one briefly.

1. For Loop

The Loops which are used for Sequential traversal are known as For Loop. The syntax of For Loop in Python Programming language is similar to the syntax of all other languages. For loop has the easiest syntax and it is the most commonly used in Python Programming Language. Given below is the syntax of For Loop in Python:-

image.png

Example of For Loop:-

  1. Using range function

We will understand the use of for loop using range function with the help of an example.

image.png

From the above code, you can see that we had used for loop with range function and we know that our range function runs from 0 to n-1 terms. So it has printed from 0 to 4 instead of 0 to 5.

2 . Using for loop in a list

We will understand the use of for loop with the help of lists.

image.png

In the above code we had used list in for loop and we will get all the elements of the list as output in separate lines respectively.

3 . Using For Loop in String

We will understand the use of for loop with the help of a string.

image.png

As it is visible from the above code, we can see that all the elements of the string are printed one by one in separate lines respectively.

While Loop

The loop which can execute a given set of statements as long as the given condition is true is known as While Loop in Python Programming language. But when the condition is false then the line immediately after the loop in the program is executed. The syntax of While Loop is also simple but it provides a better way of looping as it is used in most of the cases where there are comparisons. Let's understand the syntax of While loop in Python Programming language:-

image.png

Example of While Loop:-

  1. A general example of While Loop

We will understand the use of the While loop with the help of a basic or general example

image.png

The above-explained example is the general case of the while loop.

2 . Using While Loop with Break Statement

Let's understand the use of a While loop with a break statement with the help of an example.

image.png

As you can see from the above code that we had used a break statement which stops the code after printing 2.

3 . Using While loop with Continue Statement

Let's understand the use of the While loop with the continue statement with the help of an example.

image.png

We had used the continue statement in the above code and we can see that our code will execute and will run infinite times as it is in the loop.

Nested Loops

The function of nested loops is similar to the function of nested If- Else statements and in simple words, it means that loop inside a loop. We can use nested for loops and nested while loops. In simpler words, we can use both For Loop and While Loop together in a single program. There is no syntax in Nested Loops as they are comprised of both For loop and While loop.

  1. Nested For Loop

We will understand the use of Nested For Loop with the help of an example

image.png

From the above code, we can see that we had used two for loops and we can easily see from the output that each f_name is printed with each l_name. We can also use nested For loops to print several different patterns.

2 . Nested While Loops

We will understand the use of Nested While Loop with the help of an example

image.png

From the above code, it is clearly visible that we had used while loops which is a nested loop and it will perform the same functions which are similar to the functions of nested For loop, and will give us the output in the same way as nested for loop.

Thanks for Reading. Hope you liked it. Subscribe to our newsletter so that you will get notified of our new posts.

NOTE:- The awesome artwork used in this article was created by Farrúh Tillaev

CREDIT FOR THIS GREAT ARTICLE => Khush Mehndiratta

HAPPY LEARNING!!!

DON'T FORGET TO FOLLOW AND SUBSCRIBE FOR MY NEWSLETTER TO RECEIVE MY FUTURE POSTS