Python creates increasing sequence, decreasing sequence and arithmetic sequence

 Use the range() function

function syntax

range(start, stop, step])

Parameter Description:

  • start: optional parameter, counting starts from start, the default starts from 0. For example range(5) is equivalent to range(0, 5);
  • stop: Count up to but not including stop . For example: range (0, 5) is [0, 1, 2, 3, 4] without 5
  • step: optional parameter, step size, the default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)

example

Example 1: range(10) # from 0 to 9

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 2: range(1, 11) # from 1 to 10

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Example 3: range(0, 40, 5) # step size is 5

[0, 5, 10, 15, 20, 25, 30, 35]

Example 4: range(0, 11, 3) # step size is 3

[0, 3, 6, 9]

Example 5: range(0, -12, -1) # step size is negative

[0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11]

Example 6: range(0)

[]

Example 7: range(1, 0)

[]

 

Guess you like

Origin blog.csdn.net/weixin_46159962/article/details/128184383