Is there something like this in python?
sum += i for i in range(10)
sum should be 55
There's no one-liner for it, like there are for list comprehensions
total = 0
for i in range(10):
total += i
Don't use sum
as a variable name, it's the name of a built-in function (which happens to do what your loop does).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.