[Tutor] Re: Loop Variables

Andrei project5 at redrival.net
Fri Jun 25 02:57:53 EDT 2004

ivan low <visional_freeman <at> yahoo.com> writes:

> Hi, I had come to read a book about loop variable that have i, j, k.
> I don't understand why are there 3 type?
> What's the different?

They're the same type (integer), but three different integers so you can loop
over three different things at once. For example you might have recorded in a
sport competition for every player in a team for every game that team played how
many points the player made. If you'd want to calculate averages per player,
you'd have  to loop over all teams, within that loop have a second loop over
each player in the team and within that loop have a third loop over the list of
points scored by that player.

In Python you'll not be using as many integer loop variables as you would in
other languages though, because you can just say:

for team in competition:
    for player in team:
        for match in player.games:

where in a different language you'd do something similar to this (only even

for i in range(len(competition)):
    team = competition[i]
    for j in range(len(team)):
        player = team[i]
        for k in range(len(player.games)):
            match = player.games[k]

More information about the Tutor mailing list