""" range(stop) -> range object range(start, stop[, step]) -> range object Return an object that produces a sequence of integers from start (inclusive) to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. These are exactly the valid indices for a list of 4 elements. When step is given, it specifies the increment (or decrement). """ # range 返回的是range 对象,range(stop) var = range(5) print(var) # range(0, 5) print(type(var)) # <class ‘range‘> print(list(var)) # [0, 1, 2, 3, 4] # range(start,stop) var = range(10,15) for ele in var: print(ele) # 10 # 11 # 12 # 13 # 14 # range (start,stop,step) var = range(10,20,2) for ele in var: print(ele) # 10 # 11 # 12 # 13 # 14 # 10 # 12 # 14 # 16 # 18 # range() 和 len() 搭配,对一个序列的索引进行迭代 a = [‘This ‘, ‘is‘, ‘a‘, ‘range ‘, ‘test‘] for ele in a: print(ele) # This # is # a # range # test for e in range(len(a)): print(e,a[e]) # 0 This # 1 is # 2 a # 3 range # 4 test
原文:https://www.cnblogs.com/pickKnow/p/10930195.html