Mastery 25. Creation and use of ranges in Python

Range is a very useful built in function in Python. It allows us to create lists with arithmetic progressions.

The syntax is the following:

     range (start, stop, step)

Where the arguments have to be integer numbers. We can omit the “start” argument and Python sets it to 0 by default. We can also omit the “step” argument and it will be set to 1 by default.

It is also important to remark that the “stop” argument is never taken into account as part of the generated list.

Here are some examples:

>>range(5)

[0,1,2,3,4]

 

>>range(1,7)

[1,2,3,4,5,6]

 

>>range(20,100,10)

[20,30,40,50,60,70,80,90]

 

CC BY 4.0 Mastery 25. Creation and use of ranges in Python by Manuel Madrigal is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.