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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python 程序:計(jì)算列表中偶數(shù)和奇數(shù)的和

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

公司主營(yíng)業(yè)務(wù):成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出墨江免費(fèi)做網(wǎng)站回饋大家。

寫(xiě)一個(gè) Python 程序,使用 For 循環(huán)、While 循環(huán)和函數(shù),用一個(gè)實(shí)際例子來(lái)求列表中偶數(shù)和奇數(shù)的和。

Python 程序使用 For 循環(huán)查找列表中偶數(shù)和奇數(shù)的和

在這個(gè) python 程序中,我們使用 For 循環(huán)來(lái)迭代給定列表中的每個(gè)元素。在 Python 循環(huán)中,我們使用 If 語(yǔ)句來(lái)檢查和查找偶數(shù)和奇數(shù)的和。

# Python Program to find Sum of Even and Odd Numbers in a List

NumList = []
Even_Sum = 0
Odd_Sum = 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):
    if(NumList[j] % 2 == 0):
        Even_Sum = Even_Sum + NumList[j]
    else:
        Odd_Sum = Odd_Sum + NumList[j]

print("\nThe Sum of Even Numbers in this List =  ", Even_Sum)
print("The Sum of Odd Numbers in this List =  ", Odd_Sum)

在這個(gè) python 程序中,為了在一個(gè)列表中找到偶數(shù)和奇數(shù)的和,用戶輸入了項(xiàng)= [2,3,4,5],偶數(shù) 和= 0,奇數(shù) 和= 0。

對(duì)于循環(huán)–第一次迭代:對(duì)于范圍(0,4) 中的 0,條件為真。所以,進(jìn)入 If 語(yǔ)句

if(NumList[0]% 2 = = 0)= > if(2% 2 = = 0)–條件為真 偶數(shù) Sum =偶數(shù) Sum+NumList[0]=>0+2 = 2

第二次迭代:對(duì)于范圍(0,4)中的 1–條件為真 如果(NumList[1] % 2 == 0) = >如果(3% 2 = = 0)–條件為假,則進(jìn)入 Else 塊。 奇 和=奇 和+ NumList[1] = > 0 + 3 = 3

第三次迭代:對(duì)于范圍(0,4)中的 2–條件為真 如果(NumList[2] % 2 == 0) = >如果(4% 2 = = 0)–條件為真 偶數(shù) _Sum = 2 + 4 = 6

第四次迭代:對(duì)于范圍(0,4)中的 3–條件為真 如果(5% 2 = = 0)–條件為假,則進(jìn)入否則塊。 奇數(shù) _ 總和= 3 + 5 = 8

第五次迭代:對(duì)于范圍(4)中的 4–條件為假。因此, Python 退出 For 循環(huán)

Python 程序使用 While 循環(huán)查找列表中偶數(shù)和奇數(shù)的和

這個(gè)計(jì)算偶數(shù)和奇數(shù)之和的 Python 程序同上。我們剛剛將 For Loop 替換為 While loop 。

# Python Program to find Sum of Even and Odd Numbers in a List

NumList = []
Even_Sum = 0
Odd_Sum = 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):
    if(NumList[j] % 2 == 0):
        Even_Sum = Even_Sum + NumList[j]
    else:
        Odd_Sum = Odd_Sum + NumList[j]
    j = j+ 1

print("\nThe Sum of Even Numbers in this List =  ", Even_Sum)
print("The Sum of Odd Numbers in this List =  ", Odd_Sum)

使用 while 循環(huán)輸出的 Python 列表中偶數(shù)和奇數(shù)的總和

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 22
Please enter the Value of 2 Element : 33
Please enter the Value of 3 Element : 44
Please enter the Value of 4 Element : 55
Please enter the Value of 5 Element : 99

The Sum of Even Numbers in this List =   66
The Sum of Odd Numbers in this List =   187

用函數(shù)計(jì)算列表中偶數(shù)和奇數(shù)之和的 Python 程序

這個(gè) Python 奇數(shù)和偶數(shù)的和列表程序與第一個(gè)示例相同。但是,我們使用函數(shù)來(lái)分離邏輯

# Python Program to find Sum of Even and Odd Numbers in a List

def even_sum(NumList):
    Even_Sum = 0
    for j in range(Number):
        if(NumList[j] % 2 == 0):
            Even_Sum = Even_Sum + NumList[j]
    return Even_Sum

def odd_sum(NumList):
    Odd_Sum = 0
    for j in range(Number):
        if(NumList[j] % 2 != 0):
            Odd_Sum = Odd_Sum + NumList[j]
    return Odd_Sum

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)

Even_Sum = even_sum(NumList)
Odd_Sum = odd_sum(NumList)
print("\nThe Sum of Even Numbers in this List =  ", Even_Sum)
print("The Sum of Odd Numbers in this List =  ", Odd_Sum)

使用函數(shù)輸出的列表中偶數(shù)和奇數(shù)的總和

Please enter the Total Number of List Elements: 7
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 9
Please enter the Value of 3 Element : 21
Please enter the Value of 4 Element : 13
Please enter the Value of 5 Element : 87
Please enter the Value of 6 Element : 14
Please enter the Value of 7 Element : 66

The Sum of Even Numbers in this List =   92
The Sum of Odd Numbers in this List =   130

文章名稱:Python 程序:計(jì)算列表中偶數(shù)和奇數(shù)的和
瀏覽路徑:http://www.5511xx.com/article/dpsoesp.html