Data Science Certification Training with Pyth ...
- 86k Enrolled Learners
- Live Class
Python programming language comes with in-built data types like list, dictionary, set, tuple, etc. Range in python is another in-built python datatype which is mainly used with loops in python. It returns a sequence of numbers specified in the function arguments. In this article, we will learn about the range in python in detail with various examples. Following are the topics covered in this blog:
It is an in-built function in Python which returns a sequence of numbers starting from 0 and increments to 1 until it reaches a specified number. The most common use of range function is to iterate sequence type. It is most commonly used in for and while loops.
Following are the range function parameters that we use in python:
range(start, stop, step)
Below is an example of how we can use range function in a for loop. This program will print the even numbers starting from 2 until 20.
for i in range(2,20,2): print(i)
Output: 2 4 6 8 10 12 14 16 18
We can use range in python to increment and decrement step values using positive and negative integers, following program shows how we can get the sequence of numbers in both the orders using positive and negative steps values.
for i in range(2, 20, 5): print(i, end=", ") for j in range(25, 0 , -5): print(j , end=", ")
Output: 2, 7, 12, 17, 25, 20, 15, 10, 5
The range function does not support float or non-integer numbers in the function but there are ways to get around this and still get a sequence with floating-point values. The following program shows an approach that we can follow to use float in range.
def frange(start , stop, step): i = start while i < stop: yield i i += step for i in frange(0.6, 1.0, 0.1): print(i , end=",")
Output: 0.6, 0.7, 0.8, 0.9
The following program shows how we can reverse range in python. It will return the list of first 5 natural numbers in reverse.
for i in range(5, 0, -1): print(i, end=", ")
Output: 5, 4, 3, 2, 1, 0
In the program below, there is a concatenation between two range functions.
from itertools import chain res = chain(range(10) , range(10, 15)) for i in res: print(i , end=", ")
Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
The following program shows how we can access range using indexes.
a = range(0,10) b = range(0,10) print(a) print(b)
Output: 3 5
The following program shows how we can simply convert the range to list using type conversion.
a = range(0,10) b = list(a) c = list(range(0,5)) print(b) print(c)
Output: [0,1,2,3,4,5,6,7,8,9] [0,1,2,3,4]
This brings us to the end of this article where we have learned how we can use range in python with several examples including a for loop in python and difference between range and xrange in python. I hope you are clear with all that has been shared with you in this tutorial.
If you found this article on “Range In Python” relevant, check out the Edureka Python Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Python developer. The course is designed to give you a head start into Python programming and train you for both core and advanced Python concepts along with various Python frameworks like Django.
If you come across any questions, feel free to ask all your questions in the comments section of “Range In Python” and our team will be glad to answer.