Python range() is one of the built-in function which is used for performing operations with a sequence of numbers. In this tutorial, we will take a look into range() function with simple examples and also some advantages of using this particular function in your Python development.
Let’s get started!
What is range() function in Python?
The range() method in Python web development is a built-in function that returns the object which is a sequence of integers. Python range() generates the numbers between the given start integer and the stop integer, which can be used in iterating over the loop.
range(start, stop[, step])
The range() function takes three arguments. Start and Step are the optional arguments. The first argument is a lower limit denoting at which position to start. The default value is 0 if not specified. The second argument is an upper limit which denotes numbers generated up to this value. This function doesn’t include this value in the result. The last argument is a difference between each number in the result. If not specified, the default value of the step parameter is 1.
Example
Only one argument in range() function.
>> print("Printing first 15 values using range(): ") >> for i in range(15): >> print(i, end=', ')
Output
>> Printing first 15 values using range(): >> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
Example
Two arguments in range() function.
>> print("The numbers between the start and stop values using range(): ") >> for i in range(5, 10): >> print(i, end=', ')
Output
>> The numbers between the start and stop values using range(): >> 5, 6, 7, 8, 9,
Example
Three arguments in range() function.
>> print(“The even numbers between 1 and 10 using range(): ") >> for i in range(1, 10, 2): >> print(i, end=', ')
Output
>> The even numbers between 1 and 10 using range(): >> 2, 4, 6, 8,
Rules for range() function arguments
The range() function only works with integers i.e., whole numbers. All arguments must be integers. You can not pass a string or float number or any other type in a start, stop and step argument of a range(). Ensure that step value is not zero. Python range() function raises a ValueError exception, if a step value is 0.
Example
Decrementing with range() using a -ve step.
>> print("Negative numbers range(): ") >> for number in range(-2, -10, -2): >> print(number, end=', ')
Output
>> Negative numbers range(): >> -2, -4, -6, -8,
Example
Decrementing with range() from –ve to +ve number.
>> print ("Printing range from negative to positive numbers: ") >> for num in range(-2,5,1): >> print(num, end=", ")
Output
>> Printing range from negative to positive numbers: >> -2, -1, 0, 1, 2, 3, 4,
Example
Generating values with range() from +ve to -ve number.
>> print ("Generating values with range() from positive to negative: ") >> for num in range(2,-5,-1): >> print(num, end=", ")
Output
>> Generating values with range() from positive to negative: >> 2, 1, 0, -1, -2, -3, -4,
Reverse range()
One of the most interesting features about using python range() method is, we can generate a sequence of integers in a descending or reverse order by using a negative step value. To enable this feature, we need to set the step argument of the range() method to value -1.
Example
Creating values with reverse order.
>> print ("The numbers with reverse order are: ") >> for number in range(4,-1,-1): >> print (number, end=', ')
Output
>> The numbers with reverse order are: >> 4, 3, 2, 1, 0
Advantages of Python range()
Using range() function, we can create a list in python. Python V3’s range uses the generator, by using python V3’s range() we can generate value as when for loop iteration asked for it. i.e., the range() doesn’t produce all numbers at once.
We can convert the range() output into the list since the range() function returns an immutable sequence of integers. Use the list method to convert range output to list.
>> print("Python range() converted to list : ") >> odd_numbers = list(range(0,10,2)) >> print("Printing list", odd_numbers)
Output
>> Python range() converted to list : >> Printing list [1, 3, 5, 7, 9]
Python range() Vs xrange() functions
When compared to range() function, xrange() also returns the generator object which can be used to iterate numbers only by looping. The main disadvantage of xrange() is that only a particular range is displayed on demand and hence it is “lazy evaluation“.
Example
Using xrange() function
>> xr = xrange(1, 10, 1) >> type(xr) >> for num in xr: … print(num) 1 2 3 … 9
Hope you find this helpful. If you are a Python beginner, by now you would have a clear idea of Python range() method. I’ve covered all the basic usage of this function with sufficient and simple examples. If you have any further doubts or suggestions comment it below. We will get back to you soon.
Like what you’re reading? Need more updates on development technologies? Subscribe to our exclusive weekly newsletter.
Stuck on starting your Python Project? Get started immediately with our team of Python experts at Agira. Hire your Python developer now!