Page 301 - Beginning Programming with Pyth - John Paul Mueller
P. 301
FIGURE 13-8: Leaving the beginning number of a range blank prints from element 0.
Even though doing so is really confusing, you can use negative indexes with Python. Instead of working from the left, Python will work from the right and backward. For example, if you have List1 = ["One", 1, "Two", True] and type List1[-2], you get Two as output. Likewise, typing List[-3] results in an output of 1. The rightmost element is element -1 in this case.
Looping through Lists
To automate the processing of list elements, you need some way to loop through the list. The easiest way to perform this task is to rely on a for statement, as described in the following steps.
1. Type the following code into the window — pressing Enter after
each line:
List1 = [0, 1, 2, 3, 4, 5]
for Item in List1:
print(Item)
The example begins by creating a list consisting of numeric values. It then uses a for loop to obtain each element in turn and print it onscreen.
2. Click Run Cell.
Python shows the individual values in the list, one on each line, as shown in Figure 13-9.