1 回答

TA貢獻4條經驗 獲得超2個贊
class range(object)
?|? 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).
在python環境下輸入help(range)可以看到這個對象的內部定義就是從開始到結尾的?
現在你的問題中1比10小 就會不輸出
相反 在python隊列等可迭代對象的操作中,-1就有意義了
比如listA = [0,1,2,3,4,5,6,7,8,9]
listA[-1] 表示的是從列表尾部最后一個 即為9
listA[-2] 表示倒數第二個 為8
所以前者不輸出,后者輸出
添加回答
舉報