# [Tutor] need futher explaining

wesley chun wescpy at gmail.com
Fri Aug 10 09:44:45 CEST 2007

```> names = ['anne', 'beth', 'george', 'damon']
> ages = [12, 45, 32, 102]
> for i in range(len(names)):
> print names[i], 'is', ages[i], 'years old'
>
> now all of it makes sense to me except for the line for i in range(len(names)):
> the len statement calculates the number of characters

hi, and welcome to python!

first of all len() is built-in function and not a statement.  the way
you can tell is that if it has parentheses after it, i.e., "len()",
then it is usually a function.  a statement will not have that, i.e.,
print, if, for, etc.  those are statements.

minor semantics... anyway, len() can be used to find out how many
things there are in an object.  as you've pointed out, len() can be
used to determine how many characters are in a string.  but len() can
also be used to find out how many objects are in a list, tuple, or
dictionary (dict):

>>> names = ['anne', 'beth', 'george', 'damon']
>>> len(names)
4

the example above is less clear to beginners because there is also the
complication added due to the the call to the range() built-in
function.  range() works like this:

>>> range(4)
[0, 1, 2, 3]
>>> range(2, 6)
[2, 3, 4, 5]
>>> range(2, 10, 2)
[2, 4, 6, 8]

with a single argument, range() will create a list of numbers from 0
up to but not including the number, as in range(4) above.

with a pair of parameters, range() will create a list of numbers from
the first number up to but not including the 2nd number, as in
range(2, 6).

finally, with 3 arguments, range() will create a list of numbers from
the first number up to but not including the 2nd number, but skipping
each time by the 3rd number, as in range(2, 10, 2).

in the original example, len(names) is called first, which we saw
results in 4.  then that gets fed to range(), or effectively,
range(4).  so it's the same thing as having this for-loop:

>>> for i in range(4):
...   print names[i], 'is', ages[i], 'years old'
...

does this code make more sense to you?  hope so! kent also had a good
idea in that there is a better way to pulling out the elements of two
lists via the same index.  there is *clearly* a relationship between
the 1st name and the 1st age, as well as the 2nd name and 2nd age,
etc.

his idea is to create another list, but pairing the elements of each
list that have a relationship to each other.  that's what the zip()
built-in function does... it takes 2 lists and "zips" them up like a
zipper into a list of tuples:

>>> zip(names, ages)
[('anne', 12), ('beth', 45), ('george', 32), ('damon', 102)]

now you can iterate over the name-age pairs together using his example.

let's go back to the original example.  why did the author use a
strange syntax that would possibly confuse readers and new Python
programmers?  well, Python's for-loop is really built to iterate over
sequences of items and is less of a counting one like it is in other
languages such as C/C++, Java, and Perl.  range() was created to make
it act more like a counting loop.

a long time ago, using range(len()) was the only way to loop through a
sequence via its index instead of by element like "for name in names"
(which is the typical way of iterating through a sequence).  in this
particular case, because the author wanted to get elements of 2
different lists at the same time, he had no choice but to go with the
more confusing index route and range(len()).

a 3rd way of doing the same thing became possible starting in Python
2.3, when the enumerate() function was added to the language.  what
i've shown you above are the two different ways to iterate through a
sequence... either by element or by index.  but there are times that
you want *both*, and that's where enumerate() comes in.  it is a
special iterator that emits both an index and an element as it
traverses the sequence/iterable:

>>> for i, name in enumerate(names):
...  print "person #%d's name is %s and they are %d years old." % (i,
name, ages[i])
...
person #0's name is anne and they are 12 years old.
person #1's name is beth and they are 45 years old.
person #2's name is george and they are 32 years old.
person #3's name is damon and they are 102 years old.

for those of you who have Core Python, enumerate() is discussed in
section 8.6 along with for-loops and range().

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
```