0% found this document useful (0 votes)
2 views4 pages

Loops Comput

This document discusses how to use for loops in Python to iterate through the indexes of a sequence, using examples with a list of fruits. It emphasizes the use of the range function and the len function for general iteration, while also comparing this method to a more 'pythonic' approach of iterating directly through items. Additionally, it introduces the enumerate function for a more efficient way to access both indexes and items, although its detailed explanation is deferred for later learning.

Uploaded by

azansaid436
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Loops Comput

This document discusses how to use for loops in Python to iterate through the indexes of a sequence, using examples with a list of fruits. It emphasizes the use of the range function and the len function for general iteration, while also comparing this method to a more 'pythonic' approach of iterating directly through items. Additionally, it introduces the enumerate function for a more efficient way to access both indexes and items, although its detailed explanation is deferred for later learning.

Uploaded by

azansaid436
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/27/22, 10:45 AM 7.7.

Traversal and the for Loop: By Index — Foundations of Python Programming

You were Last Reading: 7. Iteration > 7.6 The Accumulator Pattern
Continue Reading (/books/published/fopp/Iteration/[Link]?
lastPosition=7262.423828125)

7.7. Traversal and the for Loop: By


Index
With a for loop, the loop variable is bound, on each iteration, to the next item in a sequence. Sometimes, it
is natural to think about iterating through the positions, or indexes of a sequence, rather than through the
items themselves.

For example, consider the list ['apple', 'pear', 'apricot', 'cherry', 'peach'] . ‘apple’ is at position 0,
‘pear’ at position 1, and ‘peach’ at position 4.

Thus, we can iterate through the indexes by generating a sequence of them, using the range function.

Save & Run Original - 1 of 1 Show in CodeLens

1 fruits = ['apple', 'pear', 'apricot', 'cherry', 'peach']


2 for n in range(5):
3 print(n, fruits[n])
4 ​

0 apple
1 pear
2 apricot
3 cherry
4 peach

Activity: 7.7.1 ActiveCode (ac14_6_5a)

[Link] 1/4
2/27/22, 10:45 AM 7.7. Traversal and the for Loop: By Index — Foundations of Python Programming

In order to make the iteration more general, we can use the len function to provide the bound for range .
This is a very common pattern for traversing any sequence by position. Make sure you understand why the
range function behaves correctly when using len of the string as its parameter value.

Save & Run Original - 1 of 1 Show in CodeLens

1 fruits = ['apple', 'pear', 'apricot', 'cherry', 'peach']


2 for n in range(len(fruits)):
3 print(n, fruits[n])
4 ​

0 apple
1 pear
2 apricot
3 cherry
4 peach

Activity: 7.7.2 ActiveCode (ac14_6_5)

In some other programming languages, that’s the only way to iterate through a sequence, by iterating
through the positions and extracting the items at each of the positions. Python code is often easier to read
because we don’t have to do iteration that way. Compare the iteration above with the more “pythonic”
approach below.

Save & Run Original - 1 of 1 Show in CodeLens

1 fruits = ['apple', 'pear', 'apricot', 'cherry', 'peach']


2 for fruit in fruits:
3 print(fruit)
4 ​

[Link] 2/4
2/27/22, 10:45 AM 7.7. Traversal and the for Loop: By Index — Foundations of Python Programming

apple
pear
apricot
cherry
peach

Activity: 7.7.3 ActiveCode (ac14_6_5c)

If we really want to print the indexes (positions) along with the fruit names, then iterating through the
indexes as in the previous versions is available to us. Python also provides an enumerate function which
provides a more “pythonic” way of enumerating the items in a list, but we will delay the explanation of how
to use enumerate until we cover the notions of tuple packing and unpacking
(../Tuples/[Link]#pythonic-enumeration).

Check your understanding

How many times is the letter p printed by the following statements?

s = "python"
for idx in range(len(s)):
print(s[idx % 2])

A. 0
B. 1
C. 2
D. 3
E. 6

Check Me Compare me

✔️idx % 2 is 0 whenever idx is even

Activity: 7.7.4 Multiple Choice (question14_6_1)


[Link] 3/4
2/27/22, 10:45 AM 7.7. Traversal and the for Loop: By Index — Foundations of Python Programming

([Link])

([Link])

 Completed. Well Done!

© Copyright 2017 bradleymiller. Created using Runestone ([Link] 5.5.17. | Back to top

[Link] 4/4

You might also like