Let us understand the basics of range() function in python.
Now let us understand the concept of range function() in python, below shown are the concepts along with examples that will help you guys to understand the range() in a clear way.
The range() function is a built-in-function used in python, it is used to generate a sequence of numbers. If the user wants to generate a sequence of numbers given the starting and the ending values then they can give these values to the parameter of the range() function. The range() function will then generate a sequence of numbers according to the user's requirement.
There are three parameters inside the range(): start, stop, and step. When you think about these three-parameters, these resemble a real-life scenario such as you need to set the boundaries and then walk with defined steps.
Start: Optional - An integer number that specifies where to start (Default value is 0)
Stop: Required - An integer number that specifies where to stop.
Step: Optional - An integer number that specifies how much to increment the number (Default value is 1)
The return value of the range function is a sequence of numbers depending upon the parameters defined.
1. Let us see what happens when we just print the range function with the value 10.
x = range(10)
print(x)
range(0, 10)
As seen above the value 10 indicates the stop value for the range function and the 0 value is by default added as the starting value.
2. Let us see what happens when we put the range function in a for loop.
x = range(10)
for i in x:
print(i)
0 1 2 3 4 5 6 7 8 9
Now as seen above we can get the values from 0 to 10 but excluding 10 because the stop value of the range function always returns the value -1. Hence the values 0–10.
3. Let us see what happens that we give the start and the stop value to the range function.
x = range(1, 10)
for i in x:
print(i)
1 2 3 4 5 6 7 8 9
As seen above its prints the values from 1–9 because the start value is defined as 1.
4. Let us see what happens when we give the step value to the range function.
x = range(1, 10, 2)
for i in x:
print(i)
1 3 5 7 9
As seen above the step is used to increment the numbers, in this case, the step is 2 so the number is incremented by 2.
In general, think this way the range function is like a real-life scenario:
Even though you don't declare the step variable in this case you will take one step at a time, Obviously, nobody takes two steps at a time.
Yes, the range function is a class because when you check the type of the range function using type() then you get the answer.
x = range(10)
print(type(x))
<class 'range'>
The range function does not work for floating-point values, because according to the range function the floating object cannot be interpreted as an integer. Hence make it a common rule that always specifies integer values inside a range function.
x = range(10.0, 100.5)
for i in x:
print(i)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-48-55e80efae649> in <module>() ----> 1 x = range(10.0, 100.5) 2 for i in x: 3 print(i) TypeError: 'float' object cannot be interpreted as an integer
If at all you need to solve it you can typecast it to integers, in this case
x = range(int(10.0), int(100.5))
for i in x:
print(i)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
To support this context I was doing some research on StackOverflow and then I got to see a problem there which was stated as "I want to loop from 0 to 100 but with a step of 1/2" now this is not a simple problem, because the above concept applies for this so you need to use some logic, now I have used some sort of my logic to solve this problem as shown below:
x = range(0, 100 * 2)
for i in x:
print(i * 0.5)
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 20.5 21.0 21.5 22.0 22.5 23.0 23.5 24.0 24.5 25.0 25.5 26.0 26.5 27.0 27.5 28.0 28.5 29.0 29.5 30.0 30.5 31.0 31.5 32.0 32.5 33.0 33.5 34.0 34.5 35.0 35.5 36.0 36.5 37.0 37.5 38.0 38.5 39.0 39.5 40.0 40.5 41.0 41.5 42.0 42.5 43.0 43.5 44.0 44.5 45.0 45.5 46.0 46.5 47.0 47.5 48.0 48.5 49.0 49.5 50.0 50.5 51.0 51.5 52.0 52.5 53.0 53.5 54.0 54.5 55.0 55.5 56.0 56.5 57.0 57.5 58.0 58.5 59.0 59.5 60.0 60.5 61.0 61.5 62.0 62.5 63.0 63.5 64.0 64.5 65.0 65.5 66.0 66.5 67.0 67.5 68.0 68.5 69.0 69.5 70.0 70.5 71.0 71.5 72.0 72.5 73.0 73.5 74.0 74.5 75.0 75.5 76.0 76.5 77.0 77.5 78.0 78.5 79.0 79.5 80.0 80.5 81.0 81.5 82.0 82.5 83.0 83.5 84.0 84.5 85.0 85.5 86.0 86.5 87.0 87.5 88.0 88.5 89.0 89.5 90.0 90.5 91.0 91.5 92.0 92.5 93.0 93.5 94.0 94.5 95.0 95.5 96.0 96.5 97.0 97.5 98.0 98.5 99.0 99.5
You are free to solve the above problem with your logic and provide the solution in the comment section below.
This is also a built-in-function in python which also works in the same way as the range() I think, when I tried executing it I got a weird error:
x = xrange(1, 5)
for i in x:
print(i)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-51-969120b3b060> in <module>() ----> 1 x = xrange(1, 5) 2 for i in x: 3 print(i) NameError: name 'xrange' is not defined
Then after quite a research, I came to know that the xrange is the incremental version of range according to stack overflow. One of them said that Python2 uses xrange() and Python3 uses range, so you don' t have to worry about it.
The range functions can also be used on the python list. An easy way to do this is as shown below:
names = ['India', 'Canada', 'United States', 'United Kingdom']
for i in range(1, len(names)):
print(names[i])
Canada United States United Kingdom
I think by this time you guys know how to do this. But anyways I would provide the solution to this problem. You got to just play with the step parameter in this case.
Incrementing
x = range(1, 10, 2)
for i in x:
print(i)
1 3 5 7 9
Decrementing
x = range(10, 1, -2)
for i in x:
print(i)
10 8 6 4 2
This is all you need to know about the range function in python, if you need read more about the official documentation about the range function then I recommend you to read Python's official guide:
Thank you guys for reading this short tutorial, I hope you enjoyed it, let me know about your opinion, and if you have any doubt then the comment section is all yours.