How range will react if float data type or string data type like range of Alphabets used?

Can we use range(1, 20, 0.3)?

How it work?

Also
If we use range(a, m) so is it iterated in alphabetical order?

1 Like

The range() function can be represented in three different ways, or you can think of them as three range parameters:

  • range(stop_value) : This by default considers the starting point as zero.

  • range(start_value, stop_value) : This generates the sequence based on the start and stop value.

  • range(start_value, stop_value, step_size): It generates the sequence by incrementing the start value using the step size until it reaches the stop value.

All three parameters start_value, stop_value, step_size must be integers , else it will give an error.

And range() is iteratable

So using anything other than integers in range function will result in error.

1 Like

To add to the above answer, if you want to create a range of floating point number. there is a function in numpy library called arange.

The below code shows some examples of how numpy’s arange function does this operation (we need to import numpy in order to use arange)

from numpy import arange

print("Float range using NumPy arange():")

print("\nTest 1:")
for i in arange(0.0, 1.0, 0.1):
    print(i)

print("\n\nTest 2:")
for i in arange(0.5, 5.5, 0.5):
    print(i)

print("\n\nTest 3:")
for i in arange(-1.0, 1.0, 0.5):
    print(i)

You can use the above code to see the outputs on your own. For the first for loop the output looks like below

Test 1:
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1 Like