--Originally published at Programming Fundaments
Range() is a function, it is used to generate a list of numbers. And it has a set of parameters:
- start: Starting number of the sequence.
- stop: Generate numbers up to, but not including this number.
- step: Difference between each number in the sequence.
Some note:
- All parameters must be integers.
- All parameters can be positive or negative.
- range() (and Python in general) is 0-index based, meaning list indexes start at 0, not 1. eg. The syntax to access the first element of a list is mylist[0]. Therefore the last integer generated by range() is up to, but not including, stop. For example range(0, 5) generates integers from 0 up to, but not including, 5.
Some Examples:
>>> # One parameter
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
>>> # Two parameters
>>> for i in range(3, 6):
... print(i)
...
3
4
5
>>> # Three parameters
>>> for i in range(4, 10, 2):
... print(i)
...
4
6
8
>>> # Going backwards
>>> for i in range(0, –10, –2):
... print(i)
...
0
–2
–4
–6
–8
Examples and more at: http://pythoncentral.io/pythons-range-function-explained/