【行易少儿编程】8.Python 数值列表求和训练

版权声明:本文为博主原创文章,博主QQ:289102120,博主Mobile:15891712396 https://blog.csdn.net/vinglemar/article/details/79962914
训练1:
print("1+2+3+...+10=?")

#1:
iList=[1,2,3,4,5,6,7,8,9,10]
iSum=0
for d in iList:
	iSum+=d
print("方法1",iSum)

#2
iSum=0
for d in [1,2,3,4,5,6,7,8,9,10]:
	iSum+=d
print("方法2",iSum)

#3
iList=[1,2,3,4,5,6,7,8,9,10]
iSum=0
for i in range(0,10):
	iSum+=iList[i]
print("方法3",iSum)

#4
iSum=0
for d in range(1,11):
	iSum+=d
print("方法4",iSum)


#5
iList=[1,2,3,4,5,6,7,8,9,10]
print("方法5",sum(iList))

#6
print("方法6",sum([1,2,3,4,5,6,7,8,9,10]))


#7
iList=[1,2,3,4,5,6,7,8,9,10]
iSum=0
i=0
while i<len(iList):
	iSum+=iList[i]	
	i+=1	
print("方法7",iSum)
训练2:
print("求 2,4,6,8,10,12,14,16,18,20 之和")

#1
iList=[2,4,6,8,10,12,14,16,18,20]
iSum=0
for i in range(0,10):
	iSum+=iList[i]
print("方法1",iSum)

#2
iSum=0
for i in range(2,21,2):
	iSum+=i
print("方法2",iSum)


#3
iSum=0
iList=[2,4,6,8,10,12,14,16,18,20]
i=0
while i<len(iList):
	iSum+=iList[i]
	i+=1
print("方法3",iSum)

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试