本文摘自php中文网,作者V,侵删。

目标:定义一个数字列表,并计算列表元素之和。
例如: 输入 : [12, 15, 3, 10] 输出 : 40
方法一:
1 2 3 4 5 6 7 8 | total = 0
list1 = [11, 5, 17, 18, 23]
for ele in range(0, len(list1)):
total = total + list1[ele]
print ( "列表元素之和为: " , total)
|
结果:
方法二:使用while()循环
1 2 3 4 5 6 7 8 9 10 | total = 0
ele = 0
list1 = [11, 5, 17, 18, 23]
while (ele < len(list1)):
total = total + list1[ele]
ele += 1
print ( "列表元素之和为: " , total)
|
以上实例输出结果为:
阅读剩余部分
相关阅读 >>
Python中如何django使用haystack:全文检索的框架的实例讲解
怎么利用Python输出星座
Python如何定义整数
Python学习有哪些网站
Python怎么做反爬
Python如何计算平均值
Python字符串与字典相关操作的详解
Python线程下timer对象、lock对象和rlock对象的简单介绍
学Python下什么软件
深入分析Python的多重继承
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python实现计算列表元素之和