Python range() built-in-function. Let us understand the basics of range()... | by Tanu N Prabhu | Towards Data Science

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. Also, visit my GitHub repository to find more details about the range function below:
github.com
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 as parameters of the range() function. The range() function will then generate a sequence of numbers according to the user's requirement.
The syntax of range() function is shown below:
range(start, stop, step)
There are three parameters inside the range(): start, stop, and step. When you think about these three-parameters, it resembles a real-life scenario that would be discussed down below.
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)12 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:
You start to walk — startYou make your steps (footsteps) — stepYou stop to walk because you have reached your destination — stop.
You start to walk — start
You make your steps (footsteps) — step
You stop to walk because you have reached your destination — stop.
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 specify 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-23-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 as an integer as shown below:
x = range(int(10.0), int(100.5))for i in x:    print(i)10 11 12 13 14----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---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 I think this also works in the same way as the range(), 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-30-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, I recommend you to read Python’s official guide:
docs.python.org
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.