python 中具有负步长值的范围函数的停止值背后的逻辑是什么

What's the logic behind stop value of range function with negative step value in python

我的问题很简单。 我是 python 的新手,我最近在 python 中了解了 range()。我通过一个示例程序解决了他们要求使用范围函数打印 3 的反向乘法 table 而我的代码是

for i in range(30,3,-3):
           print(i)

但是输出与我预期的不一样。输出不包括3。我不知道为什么。 我的问题是最后一个值应该是(停止值 -1),即在这种情况下为 2。所以 3 也应该被打印但不是这样。请解释原因。 我误解了逻辑吗?如果有请解释。

range() 在生成列表时排除了停止点(实际上它们 return 是一个生成器)。您应该将 3 更改为 2,然后它将包含 3

range(start, stop[, step]) 的 python 文档说明如下::

For a negative step, the contents of the range are still determined by the formula r[i] = >start + step*i, but the constraints are i >= 0 and r[i] > stop.

这意味着停止参数不包括在范围计算中。