发现了 range() 的新特性
本帖最后由 _2_ 于 2019-11-17 15:01 编辑首先,将 range(1,10) 赋值给 a ,
就可以用 a.start , a.step 和 a.stop 分别获取传入 range() 函数的起始数值、步长和结束数值
上代码:
>>> a = range(1,10)
>>> a.start
1
>>> a.step
1
>>> a.stop
10
还有 count 和 index 两个方法
>>> dir(range(1, 10, 2))
['__bool__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index', 'start', 'step', 'stop']
>>> a = range(1, 10, 2)
>>> a.count
<built-in method count of range object at 0x033F0368>
>>> a.count(2)
0
>>> a.count(3)
1
>>> a.index(3)
1 哈我的也有 zltzlt 发表于 2019-11-17 13:27
还有 count 和 index 两个方法
没弄懂那两个方法是干嘛用的 _2_ 发表于 2019-11-17 14:45
没弄懂那两个方法是干嘛用的
和元组的 index 和 count 方法作用一样 a=range(0,100,2)
print(a.start)
print(a.stop)
print(a.step)
print((a.stop-a.start)/a.step)
print(len(a))
页:
[1]