Continuing from
part 1 of this series, we'll be talking about lists and the for loop. There will be no recap, so read up on the last installment if neccessary.
Another way to create a list is with the
range function, which creates a list of integers:
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
You'll notice that this list contains the 10 first integers, starting from 0 and ending up at 9. You might perhaps have expected the numbers from 1 to 10, but this all ties in with the zero-indexing, i.e., that the index of the first element in a list is
0. However,
range can also produce other ranges, for example if you want the list to start at something other than 0, than give the number you want to start at first:
>>> range(3, 10)
[3, 4, 5, 6, 7, 8, 9]
By the way, you might be wondering what
range actually is, and the answer is that it is a built in function. We'll get back to functions, but for now let's just say that they are called by typing their name followed by the paranthesis, and the things inside the paranthesis are called arguments.
A third way to create lists, which will come in handy when we start looking at text, is to split up a string of text into smaller pieces. For example, let's define a variable holding some text:
>>> ...
« ‹ › »