新聞中心
創(chuàng)新互聯(lián)python教程:

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了襄州免費(fèi)建站歡迎大家使用!
寫一個(gè) Python 程序,用一個(gè)實(shí)際例子找到列表中元素的和。
Python 程序查找列表中元素的和
這個(gè) python 程序允許用戶輸入列表的長度。接下來,我們使用 Python For Loop 向列表中添加數(shù)字。
Python sum 函數(shù)返回列表中所有元素的 sum 。
# Python Program to find Sum of all Elements in a List
NumList = []
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
total = sum(NumList)
print("\n The Sum of All Element in this List is : ", total)不使用求和在列表中查找元素求和的程序
在這個(gè)程序中,我們使用 For Loop 來迭代這個(gè)列表中的每個(gè)元素。在循環(huán)中,我們將這些元素添加到總變量中。
NumList = []
total = 0
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
for j in range(Number):
total = total + NumList[j]
print("\n The Sum of All Element in this List is : ", total)Python 列表項(xiàng)的總和輸出
Please enter the Total Number of List Elements : 5
Please enter the Value of 1 Element : 10
Please enter the Value of 2 Element : 20
Please enter the Value of 3 Element : 30
Please enter the Value of 4 Element : 40
Please enter the Value of 5 Element : 55
The Sum of All Element in this List is : 155使用 While 循環(huán)計(jì)算列表中元素總和的 Python 程序
這個(gè)返回列表項(xiàng)總和的 Python 程序與上面的相同。我們剛剛將 For 循環(huán)替換為 While 循環(huán)。
NumList = []
total = 0
j = 0
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
while(j < Number):
total = total + NumList[j]
j = j + 1
print("\n The Sum of All Element in this List is : ", total)使用 while 循環(huán)輸出的列表項(xiàng)的總和
Please enter the Total Number of List Elements : 6
Please enter the Value of 1 Element : 10
Please enter the Value of 2 Element : 20
Please enter the Value of 3 Element : -30
Please enter the Value of 4 Element : -40
Please enter the Value of 5 Element : 50
Please enter the Value of 6 Element : 100
The Sum of All Element in this List is : 110使用函數(shù)計(jì)算列表中所有元素總和的 Python 程序
這個(gè)求列表項(xiàng)總和的程序和第一個(gè)例子一樣。但是,我們使用函數(shù)分離了 python 程序邏輯。
def sum_of_list(NumList):
total = 0
for j in range(Number):
total = total + NumList[j]
return total
NumList = []
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
total = sum_of_list(NumList)
print("\n The Sum of All Element in this List is : ", total)使用函數(shù)輸出的列表項(xiàng)的總和
Please enter the Total Number of List Elements : 7
Please enter the Value of 1 Element : 19
Please enter the Value of 2 Element : 11
Please enter the Value of 3 Element : 32
Please enter the Value of 4 Element : 86
Please enter the Value of 5 Element : 567
Please enter the Value of 6 Element : 32
Please enter the Value of 7 Element : 9
The Sum of All Element in this List is : 756 本文標(biāo)題:Python 程序:計(jì)算列表中元素的和
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/djihgho.html


咨詢
建站咨詢
