Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在理解这个简单的 for 循环代码时遇到了一些麻烦。我只需要帮助解释为什么它会以这种方式输出。
y=0 for x in range(5): y=y+x print y >>> 0 1 3 6 10 >>>
range(5)给你
range(5)
[0,1,2,3,4]
在 for 循环中你加起来
y(0) = y(0) + x(0) >>> 0 y(1) = y(0) + x(1) >>> 1 y(3) = y(1) + x(2) >>> 3 y(6) = y(3) + x(3) >>> 6 y(10) = y(6) + x(4) >>> 10