Tag Archives: #VML

Creation and use of Ranges. Mastery 25.

The range function of Python allow us to create that, ranges. A sequence of numbers. It can be used with one, two or three parameters. Examples to be explained.

One parameter:

A one parameter range, is written by putting the word range followed by a parenthesis with a number. Example:

But, what numbers does this range contain inside of it? Let’s use the list function to find out…

We can see that range(8) contains numbers from 0 to 8. Therefore, range(n) contains numbers from 0 to n-1.

Two Parameter:

A range with two parameter is written with 2 numbers separated by a coma inside the parenthesis. Example:

What numbers does this range contains inside of it? Let’s find out…

This range contains numbers from two to six. Therefore, range(a,b) contains numbers from a to b-1.

Three paratemers:

If we use three parameters, the third one represents the increment between each value of the sucesion. The first one is the initial parameter, the second one the upper value not included. Example:

This range contains number starting from 2, increasing by 3 until 24.

Ranges also work with negative numbers. The range function is used commonly alongside for loops.