WHAT IS BUILT-IN FUNCTION RANGE: A DEEPER LOOK AND HOW TO USE IN PYTHON PROGRAMMING

BUILT-IN FUNCTION RANGE: A DEEPER LOOK

Function range also has two­ and three­argument versions. As you’ve seen, range’s one-­argument version produces a sequence of consecutive integers from 0 up to, but not including, the argument’s value. Function range’s two-­argument version produces a sequence of consecutive integers from its first argument’s value up to, but not including, the second argument’s value, as in:

In [1]: for number in range(510):
...: print(number, end=' ')
...:
5 6 7 8 9

Function range’s three­argument version produces a sequence of integers from its first argument’s value up to, but not including, the second argument’s value, incrementing by the third argument’s value, which is known as the step:

In [2]: for number in range(0102):
...: print(number, end=' ')
...:
0 2 4 6 8

If the third argument is negative, the sequence progresses from the first argument’s value down to, but not including the second argument’s value, decrementing by the third argument’s value, as in:

In [3]: for number in range(100-­2):
...: print(number, end=' ')
...:
10 8 6 4 2

*

إرسال تعليق (0)
أحدث أقدم