本文摘自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中或者怎么表示
ubuntu下使用Python读取doc和docx文档的内容方法
py文件怎么执行
Python是弱类型语言吗
简述Python如何调用系统底层api播放wav文件
Python统计单词出现次数
random模块在Python哪个版本
如何用Python画简单的动物
Python可以自学吗
Python是什么
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python实现计算列表元素之和