Note 10.5.1.
Computer scientists like to count from 0
range Function
for loop, we would need to construct our own series of numbers to loop through them.
for loop controlled iteration. Even though you can use any four items, or any four integers for that matter, the conventional thing to do is to use a list of integers starting with 0. In fact, these lists are so popular that Python gives us special built-in range function that can deliver a sequence of values to the for loop. When called with one parameter, the sequence provided by range always starts with 0. If you ask for range(4), then you will get 4 values starting with 0. In other words, 0, 1, 2, and finally 3. Notice that 4 is not included since we started with 0. Likewise, range(10) provides 10 values, 0 through 9.
range function is actually a very powerful function when it comes to creating sequences of integers. It can take one, two, or three parameters. We have seen the simplest case of one parameter such as range(4) which creates [0, 1, 2, 3]. But what if we really want to have the sequence [1, 2, 3, 4]? We can do this by using a two parameter version of range where the first parameter is the starting point and the second parameter is the ending point. The evaluation of range(1,5) produces the desired sequence. What happened to the 5? In this case, we interpret the parameters of the range function to mean range(start,beyondLast), where beyondLast means an index past the last index we want. In the 2-parameter version of range, that is the last index included + 1.
range not just work like range(start, stop)? Think about it like this. Because computer scientists like to start counting at 0 instead of 1, range(N) produces a sequence of things that is N long, but the consequence of this is that the final number of the sequence is N-1. In the case of start, stop it helps to simply think that the sequence begins with start and continues as long as the number is less than stop.
range function is lazy: It produces the next element only when needed. With a regular Python 3 interpreter, printing a range does not calculate all the elements. To immediately calculate all the elements in a range, convert the range object to a list, like list(range(4)). ActiveCode is not designed to work on very long sequences, and it may allow you to be sloppy, avoiding the list function, and see the elements in the range with print(range(4)).
range works. Recall that the loop variable takes on each value in the sequence. In this case, the variable i will take on values produced by the range function. range function.
range(0,19,2). The most general form of the range is range(start, beyondLast, step). You can also create a sequence of numbers that starts big and gets smaller by using a negative value for the step parameter.
3) tells range what number to start at.2) tells range how many numbers to skip between each element in the sequence.range(2, 5, 8)2) tells range where to start, the second number tells range where to end (before 5) and the third number tells range how many numbers to skip between elements (8). Since 10 >= 5, there is only one number in this sequence.range(2, 8, 3)range(2, 10, 3)range(8, 1, -3)range(4)
range(5, 25, 5)range(20, 3, -5)range(20, 5, 4)range(20, 5, -5)5). The second parameter must always be past the end of the range sequence.12) in range(2, 12, 4) be replaced with and generate exactly the same sequence?