Today in this tutorial, we are going to discuss the Python range() method.
range() method is widely used in Python in for loops for traversing or iterating through any sequence.
The Python range() Method
Rather than being a function, the
range() is actually an immutable sequence type. It returns a sequence of numbers of type range.
The syntax for using the Python
range() function is given below.
range(start, stop[, step])
- start(optional) is the starting number from which the sequence generation would start. It is included in the sequence and if not mentioned is by default set to 0,
- stop is the number before which the sequence generation would stop(exclusive),
- step(optional) is the step jump the function would to take while sequence generation. If not provided, it is by default considered 1.
Using range() Method in Python
Now let us look at the various ways we can actually use the Python
1. With one parameter
The two parameters,
start are optional and are by default set to 1 and 0 respectively. But for a sequence generation, the stop parameter is mandatory.
When only stop is mentioned, the
range() function creates a sequence ranging from 0 to (stop-1) with step 1. Look at the example below.
#range() with one parameter print("Type of object returned by range: ", type(range(5))) list1 = list(range(5)) print("sequence generated by range() with 1 parameter: ", list1)
Type of object returned by range: <class 'range'> sequence generated by range() with 1 parameter: [0, 1, 2, 3, 4]
As we can see, the type of sequence generated by the method is a member of the class
range. Type-casting the
range() output gives us a list containing the values 0 to 4(5-1) with step 1, as desired.
2. With two parameters
Similarly, we can use the
range() method with two parameters. In this case, the step parameter is set to 1 by default.
The example explains it easily.
#range() with two parameter list1 = list(range(3,7)) print("sequence generated by range() with 2 parameter: ", list1)
sequence generated by range() with 2 parameter: [3, 4, 5, 6]
It is clear from the output that
step is set to 0.
3. With three parameters
When all the parameters are mentioned, the
range() function generates a sequence ranging from start to stop-1. The value of each element after the start value is calculated as the sum of the previous element and the step.
The example below illustrates this fact very well.
#range() with three parameter list1 = list(range(3,20,3)) print("sequence generated by range() with 3 parameter: ", list1)
sequence generated by range() with 3 parameter: [3, 6, 9, 12, 15, 18]
From the output, it is clear that the sequence is generated with values in range 3 to 19(20-1). For the last element, just because 18+3=21 exceeds the stop(20) the sequence generation is terminated at 18.
Using range() Method with for loop in Python
As we mentioned earlier,
range() is widely used in
for loop structures. Let us look at an easy example.
#range() with for loop for i in range(1,5): for j in range(1,i+1): print(j , end="") print()
1 12 123 1234
In the above code, we try to print a pattern with each row having the numbers from the sequence returned by the
range(1,i+1) method in the inner loop. For the last iteration of the outer loop(i=4) the inner loop iterates for values of j from 1 to (4+1)-1 =4.
Hence, the output is justified.
So in this tutorial, we understood the concept of the
range() method in Python. For any further questions, feel free to use the comments below.