/    /  Python – Ranges

Python – Ranges:

Range a kind of data type in python which is an immutable. Range will be used in for loops for number of iterations. Range is a constructor which takes arguments and those must be integers. Below is the syntax.

Syntax:

class range(stop)

class range(start, stop[, step])

stop: the value of stop parameter.

step: the value of step parameter. If the value is omitted, it defaults to 1.

start: the value of start parameter. If the value is omitted, it defaults to zero.

Examples:

>>> list(range(5))
[0, 1, 2, 3, 4]

>>> list(range(10,20))
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> list(range(10,20,5))
[10, 15]

>>> list(range(10,20,2))
[10, 12, 14, 16, 18]
# It will not take the float numbers

>>> list(range(0,0.1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

>>> list(range(0,2))
[0, 1]

>>> list(range(0,1))
[0]

>>> list(range(0,10,5))
[0, 5]