Python range() loop function use 7 ways.

Introduction

Just an image of Python range() loop function.

In this article, will see the “7 ways to use Python range() loop function. range() function is to used with for loop for execute the number of sequence in a given range.

In Python 2, the range() and xrange() functions are used for the two different purposes. range() function gives the output into list, and xrange returns object.

But in Python 3 the xrange() function is not there and the range() function has the same functionality like xrange().


Syntax for Python range().

range(start, stop, step)

In the range() function have 3 arguments start, stop, and step. It only allows us the integer value, floating numbers, strings, and other type of values are not allowed.

  1. start : It is a starting position only allows integer values. The default value is 0.
  2. stop : It is at the ending position only allows integer.
  3. step : The default value is 1, and it is to increment the number only allows integer value.

So the 7 ways to use python range() loop function is as follows,

  1. Use range(stop).
  2. Python range(start, stop)
  3. Python range(start, stop, step)
  4. Increment the integer with the positive step.
  5. Decrement the integer with the negative step.
  6. Access the index value of the Python range() function.
  7. for loop in python range function.

1. Use range(stop).

Suppose we have to print the value 0 to 5 then we will use range(stop) function.

>>> print(list(range(6)))
[0, 1, 2, 3, 4, 5]
>>> 

2. Use range(start, stop)

Suppose we have to print the value between 5 to 10, then we will use range(start, stop) function.

>>> print(list(range(5,11)))
[5, 6, 7, 8, 9, 10]
>>>

3. Use range(start, stop, step).

Now we have to print the range between 1 to 10 but it should increment by 2. So the result should be like 1, 3, 5, and, .. so on.

>>> print(list(range(1,11,2)))
[1, 3, 5, 7, 9]
>>>

4. Python range() to Increment the integer with the positive step.

This is the same as the above example, lets take range of 1 to 30 in this example and increment by 5.

>>> print(list(range(1,30,5)))
[1, 6, 11, 16, 21, 26]
>>>

5. Python range() to Decrement the integer with the negative step.

Now we will decrement the integer value by taking the number as -1,-2,-3, and so on.

>>> print(list(range(30,1,-2)))
[30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
>>>

In the above example we have start the value from 30, end the value on 1, and decrement the value by -2.


6. Access the index value of the Python range() function.

Now let’s suppose we have to print the numbers from 0 to 10, so we have to use the range(11).

So we can access the value by the index number as well, let’s print the numbers first.

>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

The result came start is from 0 and the end position is 9.

So let’s find the last element in list, as we know the last position number is 9.

>>> last_element = range(10)[-1]; print(last_element);
9
>>>

To get the last element we have used -1 as a index value. Now let’s see which is the first element.

>>> first_element = range(10)[0]; print(first_element);
0
>>>

So the first element is Zero(0), now let’s see the 7 element in the list.

>>> seventh_element = range(10)[6]; print(seventh_element);
6
>>>

If we have to access the 7th element then put the index value as 6.


7. for loop in python range function.

We can use for loop with the range() function. Let’s understand with some examples.

Syntax: 
for item in range(stop):
statements
>>> for ran in range(5):
...     print(ran)
... 
0
1
2
3
4
>>>

Now suppose we have to print the numbers in the range of 1 to 9, then we will use the range(1,10).

Syntax: 
for item in range(start, stop):
statements
>>> for ran in range(1,10):
...     print(ran)
... 
1
2
3
4
5
6
7
8
9
>>>

Now suppose we have to do increment with 2, then use range(1,10,2).

Syntax: 
for item in range(start, stop, step):
statements
>>> for ran in range(1,10,2):
...     print(ran)
... 
1
3
5
7
9
>>>

You want to learn How python for loops works find the article here. There is an online compiler from which you can learn Python.


Conclusion

So we have seen the 7 different ways to use the range() function. We have seen how to access the index values. How to use for loop with the range() function. What is start, stop, and step. Comment which way you like the most.

The points to remember while using range() function.

  1. It only support the integer value. Not support the values like float, string, other type of value.
  2. There is a three arguments start, stop, and step.
  3. Start is the starting position, the default value is 0. this argument is a optional.
  4. Stop is the end position, this is an required argument.
  5. Step is the value that you have to increment by.
  6. We can use the negative step in range() function.
  7. We can access the element by using the index values.

Give your valuable time