December 2018
Beginner to intermediate
796 pages
19h 54m
English
Now we have all the tools to iterate over a sequence, so let's build on that example:
# simple.for.2.pysurnames = ['Rivest', 'Shamir', 'Adleman']
for position in range(len(surnames)):
print(position, surnames[position])
The preceding code adds a little bit of complexity to the game. Execution will show this result:
$ python simple.for.2.py0 Rivest1 Shamir2 Adleman
Let's use the inside-out technique to break it down, OK? We start from the innermost part of what we're trying to understand, and we expand outward. So, len(surnames) is the length of the surnames list: 3. Therefore, range(len(surnames)) is actually transformed into range(3). This gives us the range [0, 3), which is basically a sequence (0, 1, 2). This ...
Read now
Unlock full access