在Python中,可以使用内置函数sum()
来计算列表中元素的和。
例如,假设你有一个包含数字的列表numbers
,可以这样计算它的和:
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)
输出结果将会是15
,因为1 + 2 + 3 + 4 + 5 = 15。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: Python arange函数的性能如何