Lesson
The For Statement
Even though the while statement is great for recursive loops, it isn't good for iterator-based, recursive loops. This is where the for statement comes in. The for statement loops based a collection of items, putting each item into a temporary variable for use. We'll also need to use the in statement for syntactic sugar as demonstrated below.
>>> for spam in ("A", "B", "C", "D", "..."):
... print(spam)
...
A
B
C
D
...
As you can see, the temporary variable that holds the item from a sequence or collection follows after the for statement. In this case, spam is the temporary variable. After that, the in statement is required, followed by the sequence or collection.
The way for is used in Python and C-based languages differs significantly. Since Python's for 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 range() 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 range() .
>>> for orange in range(10):
... print(orange)
...
0
1
2
3
4
5
6
7
8
9
>>> for juice in range(2, 10):
... print(juice)
...
2
3
4
5
6
7
8
9
The range() function itself is a bit advanced, so I showed its two most basic forms. When only one parameter is used, the new range is from 0 and stops at the parameter. In the case of range(10) , 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, range(2, 10) is 2 to 9.
A little follow up on the range() function. Although this function creates an iterator list, it isn't an iterator by itself. The range() function is actually called a generator. A generator is an object that creates a group of iterator items when needed. Since they do this, there iterator lists are temporary; they do not stay in the computers memory forever.
The Else Statement
Much like the while statement, the for statement's else 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.
>>> for blue in (1, 2, 3, 4, 5, 6, 7):
... print(blue % 2)
... else:
... print("The for loop was a success!")
...
1
0
1
0
1
0
1
The for loop was a success!
The Break Statement
The break statement works the same for the for statement as it did with the while statement. It will completely end the loop prematurely, which might be helpful in some cases. Again, another brief example is given below.
>>> for berry in (1, 2, 3, 4, 5, 6, 7, 8):
... print(berry % 4)
... if berry % 4 == 0:
... break
... else:
... print("The for loop didn't end prematurely!")
...
1
2
3
0
The Continue Statement
The continue statement acts just like it does with the while statement. It will stop the current execution of code and go back to the beginning of the loop. A short example is given below.
>>> for eggs in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10):
... if eggs % 2 == 0:
... continue
... print(eggs % 2)
...
1
1
1
1
1
To summarize from the previous lesson, the continue statement can be used to increase readability and reduce large amounts of indentation.
|