Python Concepts/For Statement

Objective

 * Learn about Python's iterator-based loop statement,.
 * Learn about three dependent statements,,  , and.

Page Index

 * For Statement:Lesson
 * For Statement:Examples of  loops
 * For Statement:Assignments
 * For Statement:References

The For Statement
Even though the  statement is great for recursive loops, it isn't good for  iterator-based, recursive loops. This is where the  statement comes in. The  statement loops are based on a collection of items, putting each item into a temporary variable for use. We'll also need to use the  statement for syntactic sugar as demonstrated below.

As you can see, the temporary variable that holds the item from a sequence or collection follows the  statement. In this case,  is the temporary variable. After that, the  statement is required, followed by the sequence or collection.

The way  is used in Python and C-based languages differs significantly. Since Python's  statement is iterator-based, how would we have the temporary variable hold a number that increments on every loop? You could make a large tuple to hold each number, but this would waste a lot of memory and time. The built-in function  can help us here. This function creates an iterator list of numbers, without all the typing or memory waste. There's an example below demonstrating the uses of.

The  function itself is a bit advanced, so its two most basic forms are shown here. When only one parameter is used, the new range is from 0 and stops at the parameter. In the case of, the range is from 0 to 9.

The second example uses two parameters, the first is the number to start from, instead of a default 0. The second parameter is the number to stop. So like the example,  is 2 to 9.

A little follow up on the  function. Although this function creates an iterator list, it isn't an iterator by itself. The  function is actually called a generator. A generator is an object that creates a group of iterator items when needed. Since they do this, their iterator lists are temporary; they do not stay in the computer's memory forever.

The Else Statement
Much like the  statement, the   statement's   is executed if the loop doesn't end prematurely for any reason: errors, special keyboard keys, other statements, et cetera. A brief example is given below.

The Break Statement
The  statement works the same for the   statement as it did with the   statement. It will completely end the loop prematurely, which might be helpful in some cases. Again, another brief example is given below.

The Continue Statement
The  statement acts just like it does with the   statement. It will stop the current execution of code and go back to the beginning of the loop. A short example is given below.

To summarize from the previous lesson, the  statement can be used to increase readability and reduce large amounts of indentation.

Examples of loops
Print location and contents of each position in a list: a[0] = 0

a[1] = 1

a[2] = -2

a[3] = 3

a[4] = -4

a[5] = -5

a[6] = -6

a[7] = -7

a[8] = 8

a[9] = -9

a[10] = 10

a[11] = 11

a[12] = -12

or: Output is same as that above.

is not limited to two inputs:

John: age = 21 years; weight = 165 lbs; height = 5 ft 8 in

Bill: age = 30 years; weight = 132 lbs; height = 4 ft 1 in

Jack: age = 45 years; weight = 175 lbs; height = 6 ft 1 in

Remove all negative numbers from the list:

b = [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

a = [0, 1, 3, 8, 10, 11]

with range
When  is combined with   as in the example below, speed of execution is greatly improved.

This example shows that the  statement is simple in concept, simple to implement and fast in execution.

Assignments

 * Work with the  statement and get a general feeling of how it works.
 * Test the  statement with strings, tuples, lists, sets, and dictionaries. See how they compare and find differences between them.


 * Previous Lesson: While Statement
 * This Lesson: For Statement
 * Next Lesson: Try Statement
 * Course Home Page