python basis --range

range () function creates an iterator object is generally used in a for loop.

Python3 range () function returns an iterator objects (object type), instead of a list type, so when the print does not print the list.

Python3 list () function iterator object, the range can be returned iterator objects () into a list, the list variable is returned type.

Python2  the Range () function returns a list.

 

range (start, end, step) start, end value, the step (start and end values left and right opening and closing )

range(stop)
range(start, stop[, step])
  • start: start counting from the beginning. The default is zero. E.g. range (5) is equivalent to the range (0, 5);
  • stop: stop counting to the end, but not stop. For example: range (0, 5) is [0, 1, 2, 3, 4] No 5
  • step: the step size, the default is 1. For example: range (0, 5) is equivalent to the range (0, 5, 1)

 

py3: range (10), every time you traverse time, dynamic generation of data

 
 
Range >>> (10 ) 
Range (0, 10) in the memory # a class object is generated, not the specific example, the memory can be saved

>>> list (range (10)) #list the range function returns iterable into a list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



#
Start value is not written from zero by default; end is closed interval, so take less than 10; not write default step 1, Example: >>> for I in Range (10 ): ... Print (I) ... 0 . 1 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 # start, end and step value are written, examples: >>> for I in Range (1,20,3 ): ... Print (I) . .. . 1 . 4 . 7 10 13 is 16 . 19 >>>

 

py2: range (10), all the data generated directly

>>> range (10) # 10 has been put into memory a number of 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Guess you like

Origin www.cnblogs.com/wenm1128/p/11557932.html