For Statement - Python (2016)

Python (2016)

CHAPTER 13: For Statement

Earlier, we demonstrated how important the while statement is for recursive loops. However, we had not covered its weakness—that of its use in iterator-based recursions. In computing, “iterator” is a term used to describe a method capable of doing the same action for every item in a given collection.

When talking about iterator-based recursions, the for statement takes center stage. This is used to loop based on a collection. It puts each of the items within into a temporary variable, made immediately available for use. For “syntactic sugar” (i.e., that part of the code that makes it easier for us humans to read and write it), the in statement should also be used. This is demonstrated in the following:

>>>for bacon (“A”, “B”, “C”, “D”, “...”):

... Print(bacon)

...

A

B

C

D

...

As can be seen here, the temporary variable which has the item from a collection or sequence follows the for statement. In this instance, bacon is our temporary variable. After this, the in statement is needed, preceding the collection or sequence.

The way that the for statement is used in C-based languages and Python can differ significantly. Since the version used by Python is iterator-based, it comes as a question how the programmer would have a temporary variable hold a specific number which increments on each loop. One solution would have been to make a large tuple to hold every number, but this would unnecessarily take up both time and memory. The built-in range() function is a good tool that can help us in this little puzzle. The range() function can create an iterator list of the numbers, without the need to waste both keystrokes and memory. This is an example of the range() usage:

>>>for juice in range (10):

... Print(juice)

...

0

1

2

3

4

5

6

7

8

9

>>> for jam in range (2,10):

... Print(jam)

...

2

3

4

5

6

7

8

9

All things considered, the range() function can be quite complicated. This is why the above example show it in its two most basic forms. When only a single parameter is used, then the new range starts from 0 and ends at the parameter. In the case of range(10), the range is computed from 0 to 9.

The next example makes use of two parameters. The first one signifies the start of the range, instead of the previous default (0). Like before, the second parameter is where the numbers should stop. In the example, the range(2,10) counts from 2 to 9.

While the range() function begets an iterator list, it is not by itself an iterator. The function is in reality called a “generator”. This is any object that can create a group of iterators when needed. Since it is the generator that works, the iterators are temporary—they are not meant to stay forever in the computer.

The Else Statement

Just like the while statement, the else statement of the for is executed when the loop does not prematurely end (for any reason). Any special keyboard keys, errors, and other statements (among others) would render the statement inert. Here is a short example:

>>>for eggs in (1,2,3,4,5,6):

... Print(egg%2)

... else:

... Print(“This for loop is successful”)

...

1

0

1

0

1

0

This for loop is successful

The Break Statement

This statement works for the for statement in much the same way that it works for the while statement. It completely ends the loop in a premature manner, which can be useful in some cases. Here is another demonstration:

>>>for jam in (1,2,3,4,5,6,7,8,9):

... Print(jam%4)

... If jam %5 ==0:

... Break

... Else:

... Print(“This loop did not end prematurely”)

...

1

2

3

0

The Continue Statement

This statement works in just the same way that it does when in the while statement. It is meant to stop the code’s execution, bringing everything back to the beginning of the loop. Here is another example:

>>>for spam in (1,2,3,4,5,6,7,8,9):

... if spam%2 == 0:

... Continue

... Print (spam%2)

...

1

1

1

1

1

Just as mentioned in the previous chapter, the continue statement is also used as a means to increase the code’s readability and to reduce unwieldy amounts of indentation.