Loops in Python:

Loops in Python execute in three different ways. The ways of looping are similar in terms of basic functionality but differ in syntax and condition checking time. The types of loops in Python are provided below which we’ll be using for data science.

While Loop:

This loop executes a block of statements continuously until a provide condition is satisfy. As soon as the condition becomes false, the line immediately after the loop is execute.

In python, the method of grouping implement is indentation. Statements are taken as part of a single block if they are indent by the same number of character spaces after a program construct.

Let’s look into an example:

Loops Iteration in Python for Data Science - PST Analytics

Using else statement with while loops:

The statement immediately succeeding the while loop gets execute only when the given condition becomes false. So, if we place an else statement immediately after the while loop, it will be execute only upon falsification of the while condition. One of the important things to keep in mind is that in case of break put from a loop or raising of an exception, there will be no execution.

Below is an example to make things more clear.

Loops Iteration in Python for Data Science - PST Analytics

Single Statement While Block:

This is a while block containing a single statement. In this case, the whole loop can be declared in a single line.

Loops Iteration in Python for Data Science - PST Analytics

It is recommended to avoid such loops as these are infinite in nature and we need to terminate the compiler forcefully.

For in loop:

These are there in case of sequential traversal i.e. for the purpose of traversing of a list, array or string, etc. The ‘for in’ loop is similar to ‘for each’ loop there in other languages.

Loops Iteration in Python for Data Science - PST Analytics

Iteration by Index of Sequence:

In this, we need to calculate the length of the list and then use ‘in iterate’ over the entire sequence keeping ourselves within the length of the range.

Loops Iteration in Python for Data Science - PST Analytics

Using else with for loops:

Using else statement in for loop is different than that of using it with while loop. As there is lack of condition in for loop so unlike while loop as soon as for loop gets executed the else loop will succeed the for loop into execution.

Nested Loops:

A nested loop is a loop in which one loop is housed into another loop. We should keep in mind that it is not necessary to put similar loops for nesting, any kind of loop can be put inide any other kind of loop in Python.

Control Statements in Loops:

In order to change execution from normal sequence loop control statements are there. When there is creation of a scope due to execution, all the automatic objects created by that scope are removed. Here are some of the control statements in Python.

Continue:

It is there when we need to return the control to the beginning of the loop.

Break:

There for bringing control out of the loop.

Pass:

Pass is use for writing empty loops. These statements can also be use for generating empty functions, control statements, and classes.

To learn more about loops in python for data science, you can check this and this.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.