WHAT IS FOR STATEMENT PYTHON FOR LOOP AND HOW TO USE IN PYTHON PROGRAMMING

FOR STATEMENT

The for statement allows you to repeat an action or several actions for each item in a sequence of items. For example, a string is a sequence of individual characters. Let’s display 'Programming' with its characters separated by two spaces:

In [1]: for character in 'Programming':
...: print(character, end=' ')
...:
P r o g r a m m i n g

The for statement executes as follows:

  • Upon entering the statement, it assigns the 'P' in 'Programming' to the target variable between keywords for and in—in this case, character.
  • Next, the statement in the suite executes, displaying character’s value followed by two spaces—we’ll say more about this momentarily.
  • After executing the suite, Python assigns to character the next item in the sequence (that is, the 'r' in 'Programming'), then executes the suite again.
  • This continues while there are more items in the sequence to process. In this case, the statement terminates after displaying the letter 'g', followed by two spaces.
Using the target variable in the suite, as we did here to display its value, is common but not required.

Function print’s end Keyword Argument

The built­in function print displays its argument(s), then moves the cursor to the next line. You can change this behavior with the argument end, as in

print(character, end=' ')

which displays character’s value followed by two spaces. So, all the characters display horizontally on the same line. Python calls end a keyword argument, but end itself is not a Python keyword. Keyword arguments are sometimes called named arguments. The end keyword argument is optional. If you do not include it, print uses a newline ('\n') by default. The Style Guide for Python Code recommends placing no spaces around a keyword argument’s =.

Function print’s sep Keyword Argument

You can use the keyword argument sep (short for separator) to specify the string that appears between the items that print displays. When you do not specify this argument, print uses a space character by default. Let’s display three numbers, each separated from the next by a comma and a space, rather than just a space:

In [2]: print(102030, sep=', ')
10, 20, 30

To remove the default spaces, use sep='' (that is, an empty string).

1.1 Iterables, Lists and Iterators

The sequence to the right of the for statement’s in keyword must be an iterable—that is, an object from which the for statement can take one item at a time until no more items remain. Python has other iterable sequence types besides strings. One of the most common is a list, which is a comma­-separated collection of items enclosed in square brackets ([ and ]). The following code totals five integers in a list:

In [3]: total = 0
In [4]: for number in [2­-30179]:
...: total = total + number
...:
In [5]: total
Out[5]: 25

Each sequence has an iterator. The for statement uses the iterator “behind the scenes” to get each consecutive item until there are no more to process. The iterator is like a bookmark—it always knows where it is in the sequence, so it can return the next item when it’s called upon to do so. We cover lists in detail in the “Sequences: Lists and Tuples” chapter. There, you’ll see that the order of the items in a list matters and that a list’s items are mutable (that is, modifiable).

1.2 Built-In range Function

Let’s use a for statement and the built­in range function to iterate precisely 10 times, displaying the values from 0 through 9:

In [6]: for counter in range(10):
...: print(counter, end=' ')
...:
0 1 2 3 4 5 6 7 8 9

The function call range(10) creates an iterable object that represents a sequence of consecutive integers starting from 0 and continuing up to, but not including, the argument value (10)—in this case, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. The for statement exits when it finishes processing the last integer that range produces. Iterators and iterable objects are two of Python’s functional­style programming features. We’ll introduce more of these throughout the book.

Off-By-One Errors

A common type of off­by­one error occurs when you assume that range’s argument value is included in the generated sequence. For example, if you provide 9 as range’s argument when trying to produce the sequence 0 through 9, range generates only 0 through 8.

*

Post a Comment (0)
Previous Post Next Post