Python range() Function
Video Tutorial
Overview
The range function in Python is a built-in function that is commonly used to generate a sequence of numbers. It is often used in for loops to iterate over a set of values, or to create lists and other sequences of numbers. The range function takes one to three arguments: start, stop, and step. The start argument is the starting number of the sequence (inclusive), the stop argument is the ending number of the sequence (exclusive), and the step argument is the increment between each number in the sequence.
Syntax of range() function in Python
range() function can be called in three ways:
1. range(stop)
It takes a single argument and starts at 0, increments by 1 and stops at one less than the value of “stop” argument given.
Example –
Write a program to print the first 20 whole numbers.
Output –
2. range(start,stop)
It takes two arguments: start and stop. The sequence will start from the value given at start, increment by 1 and stop at one less than the value given.
Example –
Write a program to print a sequence of integers from 5 to 9.
Output –
The above program starts with 5, increments by 1 in each execution, and stops when the value of the number variable reaches 10.
3. range(start,stop,step)
It takes three arguments and lets you decide the start and stop values and the difference between each integer in that sequence. The default value of the step is 1.
Example –
Write a program to print all the even numbers between 10 to 20 (both inclusive).
Output –
Output –
Parameters of range() function in Python
The range() function in Python can take up to three parameters:
- start – It indicates the beginning integer of the sequence you want to generate. It is optional and set to 0 by default.
- stop – It indicates the integer at which you want to stop. It is mandatory to mention and is exclusive, i.e., it is not included in the sequence generated.
- step – It indicates the difference between consecutive integers in the sequence. In other words, it specifies the value of the incrementation or decrementation step. By default, it is set to +1 and is optional.
Return Values of range() function in Python
The range() function in Python does not return a list of numbers, but rather a sequence object that generates the numbers on-the-fly as you iterate over it. This makes it memory-efficient for working with large ranges of numbers.
When you use the range() function in a loop or pass it to a function that expects a sequence of numbers, the numbers are generated on-the-fly and returned one at a time. The range() function does not create a list or store all the numbers in memory at once.
Example of range() function in Python
Suppose there is a race with multiple players, each numbered from 0 up to a certain limit, say 10. Depending on the situation, we may only want to consider a subset of players, such as those with even numbers, or those numbered between 5 and n, or perhaps we want to consider the players in reverse order, from 10 down to 0.
To accomplish this, we can use the range() function, which allows us to generate a sequence of numbers based on our specific needs. We can use range() to generate a sequence of numbers starting from 0 and going up to a certain number, or we can generate sequences of even numbers, odd numbers, or any range of numbers between two specific values.
Example –
Output –
In the example above, the range() function is utilized with one argument, which acts as the upper limit for the numbers produced. The function generates a sequence of integers beginning from 0 up to the specified number minus one, with an increment of one by default.
Thus, for the input value of 5, the range() function will generate integers starting from 0, increasing by 1 with each iteration, and stopping at 4.
Let’s take one more example –
The above example also prints the same output as above. Both the code looks different, but logically both are the same.
Output –
Exceptions
In Python, the range() function raises a TypeError if any of its arguments are of an inappropriate type. Specifically:
- If any argument is not an integer, a TypeError is raised.
- If the step argument is zero, a ValueError is raised.
- If the step argument is negative and the start argument is less than the stop argument, a ValueError is raised.
- If the step argument is positive and the start argument is greater than the stop argument, a ValueError is raised.
What is the range function in python?
- The range() function is used to generate a sequence of numbers within a specified range. It is a built-in function in Python, meaning that it is available without the need to import any external modules or libraries.
- When you call the range() function, it returns an object that represents the sequence of numbers. This object is a special type of iterable in Python called a range object, and it can be used in a for loop or with other iterable-related functions and methods.
- One potential pitfall to be aware of when using the range() function is that it can generate a very large sequence if you're not careful with your arguments. For example, if you accidentally specify a very large number for stop, your program may run out of memory or take a long time to execute. So it's important to be mindful of the range you're generating and to test your code with smaller ranges first if possible.
More Examples Using range() with for loop
Demonstration of Python range(stop)
Code
Output
In this example, we called range(5) to generate a sequence of numbers from 0 to 4 (inclusive). Since we didn't provide a start argument, the sequence started at 0 by default. The for loop then iterated over the sequence and printed each number.
Demonstration of Python range(stop)
Code
Output
In this example, we called range(5, 10) to generate a sequence of numbers starting at 5 and ending at 9 (inclusive). The for loop then iterated over the sequence and printed each number. Note that when using range(start, stop) without providing the step argument, the sequence will have a step of 1 by default.
Demonstration of Python range(start, stop, step)
Code
Output
In this example, we called range(0, 10, 2) to generate a sequence of even numbers starting at 0 and ending at 8 (inclusive), with a step of 2. The for loop then iterated over the sequence and printed each number. Note that the step argument can be positive or negative, and can be used to generate sequences that count up or down. If the step argument is negative, the start argument should be greater than the stop argument.
Example: Using Positive Step If you need the values to increase in the given range, the step value must be a positive integer.
Output –
Example: Using Negative Step If you need the values to decrease in the given range, the step value must be a negative integer. Note that your start value must be greater than the stop value; otherwise, it returns an empty sequence.
Output –
Python range() float
The range() function in Python only works with integers, not with floats. When using range(), the start, stop, and step arguments must all be integers.
If you want to generate a sequence of floating-point numbers, you can use the numpy.arange() function from the NumPy library. This function is similar to the range() function, but works with floating-point numbers.
Concatenation of two range() function
What if you want to join 2 range() results? Yes, we can do it using the chain method of the itertools module. It concatenates the results of both the range() functions.
Output –
Accessing range() with index value
The range() function returns a sequence of integers that can be accessed using indexing.
Output –
Conclusion
- Range function in Python generates a sequence of numbers, used in for loops to iterate over values or create number sequences.
- Range function in Python takes one to three arguments: start, stop, and step.
- The range function can be called in three ways: range(stop), range(start, stop), and range(start, stop, step).
- range() in Python generates numbers on-the-fly while iterating, making it memory-efficient for large ranges.
- TypeError is raised by range() function if the argument types are incorrect, while ValueError is raised if step is negative or zero and start is greater or equal to stop.