Python range() function

The Python range() function (built-in-function) is used to generate a sequence of numbers. It generates a list of integers from some lower limit (0 by default) up to some upper limit , possibly in increments (steps) of some other number (1 by default). The range() function works a little bit differently between Python 2.x and Python 3.x under the hood, however the concept is the same. Syntax
range(Stop)
range(Start, Stop)
range(Start, Stop[, Step])
  1. start - The starting point for the range() to generate numbers.
  2. stop - It is the point just before range() ends. The range of integers end at stop - 1.
  3. step (Optional) - integer value which determines the increment between each integer in the sequence.
examples
# range with only one argument; (stop – 1) upper limit. for i in range(5): print (i)
output
0,1,2,3,4


# range of numbers within 3 and 8 for i in range(3,8): print (i)
output
3,4,5,6,7

Incrementing with positive step

#range of numbers within 1 and 9, increment of 2 for i in range(1,9,2): print (i)
output
1,3,5,7


#range of numbers within 1 and 9, increment of 3 for i in range(1,9,3): print (i)
output
1,4,7

Decrementing with negative step

#range of numbers within 9 and 1, decrement of -2 for i in range(9,1,-2): print (i)
output
9,7,5,3


#range of numbers within 9 and 1, decrement of -3 for i in range(9,1,-3): print (i)
output
9,6,3

How to floats with Python's range() function

Unfortunately Python range() function doesn't support the float numbers. i.e. user cannot use floating point number in any of its argument.
For loop with range() function
If you provide a non-integer stop value, then it raises a TypeError: 'float' object cannot be interpreted as an integer . However, you can implement an alternate way to get the range of floating numbers .
def floatRane(start, stop, step): i = start while i < stop: yield i i += step for i in floatRane(0.4, 0.7, 0.1): print(i)
output
0.4, 0.5, 0.6

Python while loop using range() function


range() in Python

Concatenation of two range() functions using chain() method

The chain() method takes any number of iterables as arguments and "chains" them together.
how to Python range Step

Accessing Python range() values using index


Decrementing with python range() using a negative step

Python range() to List

The Python range type represents an immutable sequence of numbers, so it is possible to convert the output of a range() to the Python list.
Using float Numbers in Python range() function