Step argument

Range also supports a step argument:

>>> list(range(0, 10, 2))
[0, 2, 4, 6, 8]

Note that in order to use thestep argument, we must supply all three arguments. Range is curious in that it determines what its arguments mean by counting them. Providing only one argument means that the argument is the stop value. Two arguments are start and stop, and three arguments are start, stop and step. Python range() works this way so the first argument, start, can be made optional, something which isn't normally possible. Furthermore the range constructor doesn't support keyword arguments. You might almost describe it as unPythonic!

The arguably unPythonic constructor for range, where the interpretation of the arguments depends on whether one, two, or three are provided as shown in the following diagram:

Figure 5.4: UnPythonic constructor for range
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset