Friday, March 29, 2019

Shyama Sankar Vellore: Iteration in Python: The for, while, break, and continue statements

In this post, we will discuss iterations in Python. We will go over what iteration is, the two types of iterations (definite and indefinite), and the Python statements used for implementing iterations- for and while. We will also discuss the break and continue statements and see how they are used to alter the flow of an iteration.

What is an iteration?

Iteration is defined as the repetition of a process.

Let's say, we have a set of files on our computer and we wanted to append their names with a serial number. What would we do? We would rename every file one by one. So basically, we are iterating over every file and renaming them.

In programming, iteration or looping is the act of repeatedly executing a block of code.

Types of iteration

Depending on the way in which the number of iterations is determined, there are two types of iterations or loops:
  1. Definite iteration or loop
    If the number of iterations is predetermined when we start the loop, then it is called definite. For example, repeat renaming a file for 10 files in a directory.
  2. Indefinite iteration or loop
    If the number of iterations is not known when we start the loop, then it is called indefinite. For example, repeat moving the first file in a directory to another while it is not empty.
Now let us see how we can implement iteration in Python.

The while statement: Indefinite iteration in Python

The while statement is used to execute a set of statements as long as an expression evaluates to true. In Python, the while statement comes with an optional else condition which allows us to execute another set of statements if the expression evaluates to false. Let's take a look at the syntax.

Syntax


The expression is repeatedly evaluated and set_of_statements_1 is executed as long as the expression evaluates to true. Once the expression evaluates to false, then set_of_statements_2 is executed. Note that the else clause is optional. If the else block is not there, we just break out of the loop once the expression becomes false.

As you can see, we do not specify the exact number of times the loop would execute. Hence, the while statement is suited for implementing indefinite loops.

Examples

1. Basic while loop

The following example shows a basic while loop used to pop elements from a list and print them.


2. while loop with else

Now let us see how we can modify the previous example to print some message when the list becomes empty. Note that if the expression never evaluates to true, then only the code block associated with else will get executed.


3. Infinite while loop

We would end up with an infinite loop (a loop that would never end) if the expression always evaluates to true. Let us see an example.


4. Nested while loop

Loops can be nested. Let us see an example for nested-while loops.


The for statement: Definite iteration in Python

In Python, the for statement is used to iterate over the elements of an iterable object. A set of statements is executed for every item in the iterable. Similar to the while statement, the for statement also has an optional else clause. Let's see the syntax.

Syntax

The expression is evaluated once. It is expected to yield an iterable, i.e., an object that can give us an iterator when called using the built-in iter() function. The set_of_statements_1 is executed once for every element obtained from the iterator. Once the iterator raises the StopIteration exception or once it becomes empty, the loop terminates. If the else clause is provided, then set_of_statements_2 is executed before terminating the loop.

As you can see, the for loop would do a definite number of iterations, depending on how many elements are present in the iterator. So for loops are suited for implementing definite loops in Python.

Examples

1. Basic for loop

The following code snippet shows a basic for loop that iterates over a list.


2. for loop with else

The following example shows a basic for loop with an else clause.


3. Nested for loop

Now let us see an example for nested-for loops.


The break statement: Break out of a loop

The break statement can be used to terminate the execution of a loop. It can only appear within a for or while loop. It allows us to break out of the nearest enclosing loop. If the loop has an else clause, then the code block associated with it will not be executed if we use the break statement. Let us see some examples.

1. break within a while loop

Let us see how to break out of a while loop if and when a certain condition is met. We will also see how a while loop with an else behaves when a break statement is added to it. Note that the statements within the else condition are not executed if we break out of the while loop.

2. break within an infinite while loop

Now let us see how we can modify an infinite while loop to break on a certain condition.

3. break within a for loop

Now we will see how to use break within a basic for loop. Similar to while loops, we will see that the statements within else condition do not get executed if we break out of the for loop.

4. break within nested loops

Now we will see an example of nested for and while loops. Let us see how break behaves with nested loops. Note that break statement only breaks out of the nearest enclosing for or while loop.

The continue statement: Continue with the next iteration

The continue statement allows us to skip the rest of the statements within one cycle of the loop and forces it to continue to the next cycle. Let's take a look at some examples.

1. continue within a while loop

Let us see how the continue statement works in a while loop. Note that when continue is encountered in a while loop, it forces the loop to jump to the expression.

2. continue within a for loop

Now let us see how it works in a for loop. Note that if continue is encountered when we are at the last element of the iterable, then the loop is terminated or we move on to the else block if it is present.

3. continue within nested loops

The following code samples show how continue works within nested loops. Note that the continue statement causes the nearest enclosing for or while loop to continue with its next cycle.

Useful Resources and Reference



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...