Python For Loop, Nested For Loop, While Loop for beginners with practical exercise

Python generally supports two types of loops: for loop and while loop. However, a third loop[nested loop] can be generated by nesting...

 


Do you know what is loop on programming?

As a programmer, you need to know what is loop on programming and then you need to learn loop. Because the loop is one of the most important things in the programming world. Every language has loop. So, hopefully, you get a little idea about the importance of loop in programming. Now we give you an Explanation about the loop then we will learn the loop.
Loop Definition: In computer science, a loop is a programming structure that repeats a sequence of instructions until a specific condition is met. Programmers use loops to cycle through values, add sums of numbers, repeat functions, and many other things.
Loops are supported by all modern programming languages, though their implementations and syntax may differ. Two of the most common types of loops are the while loop and the for loop. In this tutorial, we will talk about these two types of loops with practical code. Let's jump to the tutorial.

What is a while loop on  Python?

Do you think ever what is a while loop and which purpose we will use a while loop?
The answer is "The while loop in Python is used to iterate over a block of code as long as the test condition is true.
We generally use this loop when we don't know the number of times to iterate beforehand."
In the while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates to True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False.
In Python, the body of the while loop is determined through indentation.
The body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True. None and 0 are interpreted as False.

What is a for loop on  Python?

The for loop in Python is used to iterate over a sequence like(list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.

Don't think about the loop. In this tutorial, we will clear everything about the loop. Just complete this tutorial carefully. Hope you will enjoy this tutorial and can understand the loop.

The while loop

A while loop is the simplest form of a programming loop. It states that while a condition is valid, keep looping. In the Python example below, the while loop will continue until i is equal to num.
The while loop requires relevant variables to be ready, in this example, we need to define an indexing variable, I, which we set to 1.


The break statement

With the break statement, we can stop the loop even if the while condition is true. That means the break statement gives you permission to stop while loop. Let's see an example:


The continue Statement

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



The else Statement

While loops have an optional else block.
The else part is executed if the condition in the while loop evaluates to False.
The while loop can be terminated with a break statement. In such cases, the else part is ignored. Hence, a while loop's else part runs if no break occurs and the condition is false. Here is an example to ensure your understand:


Here is the Output:


Here, we use a counter variable to print the string Inside loop three times.
On the fourth iteration, the condition in while becomes False. Hence, the else part is executed.

The for loop

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

This is less like the keyword in other programming languages and works more like an iterator method as found in other object-orientated programming languages.

With the for loop, we can execute a set of statements, once for each item in a list, tuple, set, etc.



The for loop does not require an indexing variable to set beforehand. That makes for loop more powerful than while loop. That's totally our opinion. You may like while. In every work, you can choose one of them. 

Looping Through a String

Even strings are iterable objects, they contain a sequence of characters. Follow this example:



The break Statement

The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
If the break statement is inside a nested loop (loop inside another loop), the break statement will terminate the innermost loop. Here is a simple example:

Exit the loop when x is "banana", but this time the break comes before the print.

The continue Statement

The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration. Follow this example:

Do not print banana.

The range() Function

To loop through a set of code a specified number of times, we can use the range() function,
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.



The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6):



The range() function defaults to increment the sequence by 1, however, it is possible to specify the increment value by adding a third parameter: range(2, 30, 3):


Else in For Loop

The else keyword in a for loop specifies a block of code to be executed when the loop is finished. Don't worry here is a example:

Print all numbers from 0 to 5, and print a message when the loop has ended.

Note: The else block will NOT be executed if the loop is stopped by a break statement.

Here is one more example:
  
                                                                      Break the loop when x is 3, and see what happens with the else block.

Nested Loop

A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop". Just remember Python gives you permission to use a loop inside a loop that's called a nested loop. That is easy just follow this example:

Print each adjective for every fruit.

The pass statement

Without proper conditions for loops cannot be empty. So, must you will get an error. To avoid this error you need to use pass statement. So, that is very important for your programming journey. Here is a simple example:




Hope you are all enjoying our tutorial and thanks for connecting with us. In upcoming tutorials, we decide to add some projects, and also we will give project solutions.