Log in / Register
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

3.2 Iterating Over a Sequence

In Python, a string is sometimes thought of as a sequence of characters. Sequences have special status in Python. You can iterate over sequences. Iteration refers to repeating the same thing over and over again. In the case of string sequences, you can write code that will be executed for each character in the string. The same code is executed for each character in a string. However, the result of executing the code might depend on the current character in the string. To iterate over each element of a sequence you may write a for loop. A for loop looks like this:

< statements before for loop >

for < variable > in < sequence >:

< body of for loop >

< statements after for loop >

In this code the <variable> is any variable name you choose. The variable will be assigned to the first element of the sequence and then the statements in the body of the for loop will be executed. Then, the variable is assigned to the second element of the sequence and the body of the for loop is repeated. This continues until no elements are left in the sequence.

If you write a for loop and try to execute it on an empty sequence, the body of the for loop is not executed even once. The for loop means just what is says: for each element of a sequence. If the sequence is zero in length then it won't execute the body at all. If there is one element in the sequence, then the body is executed once, and so on.

For loops are useful when you need to do something for every element of a sequence. Since computers are useful when dealing with large amounts of similar data, for loops are often at the center of the programs we write.

Example 3.4 Consider the following program.

1 s = i n p u t ("Please type some characters and press enter:")

2 f o r c i n s:

3 p r i n t (c)

4 p r i n t ("Done")

If the user enters how are you? the output is:












Figure 3.3 depicts what happens when executing the code of Example 3.4. Each character of the sequence is printed on a separate line. Notice that there are blank lines, or what appear to be blank lines, between the words. This is because there are space characters between each of the words in the original string and the for loop is executed once for every character of the string including the space characters. Each of these blank lines really contains one space character.

Practice 3.4 Type in the code in Example 3.4. Set a break point on theprint(c) line. Run it with the debugger and watch it as it runs. Then answerthese questions:

1. Does the string s change as the code is executed?

2. What happens if the user just presses enter when prompted instead oftyping any characters?

Fig. 3.3 A For Loop

Practice 3.5 Modify the code in Example 3.4 to print the characters to thescreen as capital letters whether the user enters capital letters or not. For instance,it would print “HOW ARE YOU?” to the screen, with one letter oneach line if “how are you?” were entered at the keyboard.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science