Python: Function range()

If you are staring into Python's world, one of the first things that you will learn is about the function range. The function range returns a list of numbers, as a default element starting from 0 I typed some examples about this function:

>>> for my_range in range(11):
...     print(my_range)
...
0
1
2
3
4
5
6
7
8
9
10

As you can see the range starts as a default 0, progressing 1 by 1 until it stops before the last number. Let's see a bit more details about this function that can receive three elements:

range(start, stop, steps)

start where the list will begin (optional)

stop where will be the last element, however, the last element won´t be showed (must be present)

step what will be the progress of the list. In other words what will be the incrementation (optional)

An example using all elements:

>>> for my_range in range(2,11,2): #start on 2, ending on 11 (but will be shown), incremented by 2 each time.
...     print(my_range)
...
2
4
6
8
10

The function range is used in most cases to irate in a for loop, here are more examples:

>>> for i in range(5):
... print("This a blog post")
...
This a blog post
This a blog post
This a blog post
This a blog post
This a blog post

In the example Below I am using the function range to add values to a list.

>>> my_list = [10, 20, 30, 40, 50]
for i in range(len(my_list)):
... my_list[i] += 5
...
print(my_list)
[15, 25, 35, 45, 55]

And what about floating points is it possible to use them in the function range?

for x in range(0, 10, 0.5):
... print(x)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

Well, as you can see it is not possible, however, we can do on this way:

my_sequence = []
for i in range(0, 100, 5):
    value = i / 10
    my_sequence.append(value)

print(my_sequence)
[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]

I hope that you enjoy
Cheers!