11.9. Integer Sequence#

  • use range function

  • range creates a sequence of numbers

  • range function returns range object which is iterable

    • range function returns range object (which is iterable) ⇒convert it to list to see the inside elements

11.9.1. Syntax#

range([start],end[,step])

  • [] means optional

  • start defaults to 0

  • end is exclusive

  • step is 1 by default

  • returns iterable

11.9.2. One argument#

list(range(5))
[0, 1, 2, 3, 4]

11.9.2.1. Use case#

a=[1,2,3,4,5,6]
list(range(len(a)))
# Note: This is like indexes of the sequence you passed if you see it carefully
[0, 1, 2, 3, 4, 5]

11.9.3. Two arguments#

list(range(3,8))
[3, 4, 5, 6, 7]

11.9.4. Three arguments#

11.9.4.1. Positive Step#

list(range(2,12,3))
[2, 5, 8, 11]

11.9.4.2. Negative Step#

list(range(20,5,-5))
[20, 15, 10]