3.8 for Statement

  • Repeat an action or several actions for each item in a sequence of items.
  • A string is a sequence of individual characters.
In [1]:
for character in 'Programming':
    print(character, end='  ')
P  r  o  g  r  a  m  m  i  n  g  
  • Upon entering the for loop, Python assigns the 'P' in 'Programming' to the target variable between keywords for and in.
  • 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.
  • Continues while there are more items in the sequence.
  • Using the target variable in the suite is common but not required.

for Statement Flowchart

Flowchart of the for statement’s flow of control.

Function print’s end Keyword Argument

  • print displays its argument(s), then moves the cursor to the next line.
  • Can change this behavior with the argument end:
    print(character, end='  ')
    
  • end is a keyword argument, but it's not a Python keyword.
  • The Style Guide for Python Code recommends placing no spaces around a keyword argument’s =.
  • Keyword arguments are sometimes called named arguments.

Function print’s sep Keyword Argument

  • Keyword argument sep (short for separator) specifies the string that appears between the items that print displays.
  • A space character by default.
  • To remove the spaces, use an empty string with no characters between its quotes.
In [2]:
print(10, 20, 30, sep=', ')
10, 20, 30

3.8.1 Iterables, Lists and Iterators

  • The sequence to the right of the for statement’s in keyword must be an iterable.
    • An object from which the for statement can take one item at a time.
  • One of the most common iterables is a list, which is a comma-separated collection of items enclosed in square brackets ([ and ]).
In [3]:
total = 0
In [4]:
for number in [2, -3, 0, 17, 9]:
    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.

3.8.2 Built-In range Function and Generators

  • Creates an iterable object that represents a sequence of consecutive integer values starting from 0 and continuing up to, but not including, the argument value.
In [6]:
for counter in range(10):
    print(counter, end=' ')
0 1 2 3 4 5 6 7 8 9 

Off-By-One Errors

A logic error known as an off-by-one error occurs when you assume that range’s argument value is included in the generated sequence.


©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 3 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.