Creation and use of ranges in Python

--Originally published at My B10g

A range in python is created with the function range(). To use this you must know it´s rules. First, if the range if not specified will begin at 0 and will reach the integer before the parameter given. For example:

for i in range(3):
...     print(i)
...
0
1
2

Second, in case you pass two parameter it will take it as beginning and end in that order, but again ending one integer before the end and increasing by one as before. For example:

for i in range(3, 6):
...     print(i)
...
3
4
5

Third, when you pass three parameters, it will proceed this way: beginning, end, steps. This will mean that it will move from the beginning the number of steps you entered until it reaches the closest one to the end. For example:

for i in range(4, 10, 2):
...     print(i)
...
4
6
8

Note: In this function you can use any integer positive or negative but you can’t use floats.

http://giphy.com/gifs/SurZxNuuGJl96

.

.

.

.

.

.

.

.

.

Hacker tip:

if you want to use floats you can use this function:

def frange(start, stop, step):
...     i = start
...     while i < stop:
...         yield i
...         i += step

Shhhhhh don’t tell anyone.

You can see the original here:http://pythoncentral.io/pythons-range-function-explained/

It isn’t a well kept secret.