方法一:
total = 0
list1 = [11, 5, 17, 18, 23]
for ele in range(0, len(list1)):
total = total + list1[ele]
print("列表元素之和为: ", total)结果:
列表元素之和为: 74
方法二:使用while()循环
total = 0
ele = 0
list1 = [11, 5, 17, 18, 23]
while(ele < len(list1)):
total = total + list1[ele]
ele += 1
print("列表元素之和为: ", total)以上实例输出结果为:
列表元素之和为: 74
方法三:使用递归
list1 = [11, 5, 17, 18, 23]
def sumOfList(list, size):
if (size == 0):
return 0
else:
return list[size - 1] + sumOfList(list, size - 1)
total = sumOfList(list1, len(list1))
print("列表元素之和为: ", total)结果:
列表元素之和为: 74
推荐教程:python教程
以上就是python实现计算列表元素之和的详细内容,更多请关注Gxlcms其它相关文章!
查看更多关于python实现计算列表元素之和的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did78780