First, the syntax is:
Range (STOP)
Range (start, STOP, STEP)
start: start counting from the beginning, starting from 0 is the default. eg: range (5) equivalent to a Range (0,5)
stop: stop counting to the end, but not stop. eg: range (0,5) of [0,1,2,3,4], no. 5
STEP: step size, default is one. eg: range (0,5) is equivalent to the range (0,5,1)
Note:
Returns: an iterator objects (object type), not a list, it does not print when printed list
list () function Object iterator, can range iterables returned () into a list, returned list variable type
II example:
example 1:
. 1 A = Range (. 5 ) 2 . 3 Print ( " Example. 1: " ) . 4 Print (A) . 5 Print ( " Range (. 5) of the return type is:% S " % type (A)) . 6 . 7 for I in Range (. 5 ): . 8 Print (I) . 9 10 # example. 1: . 11 # Range (0,. 5) 12 is # Range (. 5) of the return type is: <class 'Range'> 13 is # 0 14 # . 1 15 # 2 16 # 3 17 # 4
Example 2:
1 print(list(range(0,30,5))) #[0, 5, 10, 15, 20, 25] 2 print(list(range(5))) #[0, 1, 2, 3, 4] 3 print(list(range(0,-20))) #[] 4 print(list(range(0,-20,-5))) #[0, -5, -10, -15] 5 print(list(range(-10,0,))) #[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1] 6 print(list(range(-10,0,-1))) #[]