日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python 程序:計算列表中元素的和

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

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了襄州免費(fèi)建站歡迎大家使用!

寫一個 Python 程序,用一個實(shí)際例子找到列表中元素的和。

Python 程序查找列表中元素的和

這個 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)

不使用求和在列表中查找元素求和的程序

在這個程序中,我們使用 For Loop 來迭代這個列表中的每個元素。在循環(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)計算列表中元素總和的 Python 程序

這個返回列表項(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ù)計算列表中所有元素總和的 Python 程序

這個求列表項(xiàng)總和的程序和第一個例子一樣。但是,我們使用函數(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

網(wǎng)頁題目:Python 程序:計算列表中元素的和
轉(zhuǎn)載源于:http://www.5511xx.com/article/djihgho.html