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])
- start - The starting point for the range() to generate numbers.
- stop - It is the point just before range() ends. The range of integers end at stop - 1.
- step (Optional) - integer value which determines the increment between each integer in the sequence.
# 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.
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

Concatenation of two range() functions using chain() method
The chain() method takes any number of iterables as arguments and "chains" them together.
Accessing Python range() values using index

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.
Related Topics
- How to use Date and Time in Python
- Python Exception Handling
- How to Generate a Random Number in Python
- How to pause execution of program in Python
- How do I parse XML in Python?
- How to read and write a CSV files with Python
- Threads and Threading in Python
- Python Multithreaded Programming
- How to Convert a Python String to int
- Python filter() Function
- Difference between range() and xrange() in Python
- How to print without newline in Python?
- How to remove spaces from a string in Python
- How to get the current time in Python
- Slicing in Python
- Create a nested directory without exception | Python
- How to measure time taken between lines of code in python?
- How to concatenate two lists in Python
- How to Find the Mode in a List Using Python
- Difference Between Static and Class Methods in Python?