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

公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出墨江免費做網(wǎng)站回饋大家。
寫一個 Python 程序,使用 For 循環(huán)、While 循環(huán)和函數(shù),用一個實際例子來求列表中偶數(shù)和奇數(shù)的和。
Python 程序使用 For 循環(huán)查找列表中偶數(shù)和奇數(shù)的和
在這個 python 程序中,我們使用 For 循環(huán)來迭代給定列表中的每個元素。在 Python 循環(huán)中,我們使用 If 語句來檢查和查找偶數(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)在這個 python 程序中,為了在一個列表中找到偶數(shù)和奇數(shù)的和,用戶輸入了項= [2,3,4,5],偶數(shù) 和= 0,奇數(shù) 和= 0。
對于循環(huán)–第一次迭代:對于范圍(0,4) 中的 0,條件為真。所以,進(jìn)入 If 語句
if(NumList[0]% 2 = = 0)= > if(2% 2 = = 0)–條件為真 偶數(shù) Sum =偶數(shù) Sum+NumList[0]=>0+2 = 2
第二次迭代:對于范圍(0,4)中的 1–條件為真 如果(NumList[1] % 2 == 0) = >如果(3% 2 = = 0)–條件為假,則進(jìn)入 Else 塊。 奇 和=奇 和+ NumList[1] = > 0 + 3 = 3
第三次迭代:對于范圍(0,4)中的 2–條件為真 如果(NumList[2] % 2 == 0) = >如果(4% 2 = = 0)–條件為真 偶數(shù) _Sum = 2 + 4 = 6
第四次迭代:對于范圍(0,4)中的 3–條件為真 如果(5% 2 = = 0)–條件為假,則進(jìn)入否則塊。 奇數(shù) _ 總和= 3 + 5 = 8
第五次迭代:對于范圍(4)中的 4–條件為假。因此, Python 退出 For 循環(huán)
Python 程序使用 While 循環(huán)查找列表中偶數(shù)和奇數(shù)的和
這個計算偶數(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ù)計算列表中偶數(shù)和奇數(shù)之和的 Python 程序
這個 Python 奇數(shù)和偶數(shù)的和列表程序與第一個示例相同。但是,我們使用函數(shù)來分離邏輯
# 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 本文標(biāo)題:Python 程序:計算列表中偶數(shù)和奇數(shù)的和
轉(zhuǎn)載來于:http://fisionsoft.com.cn/article/dpsoesp.html


咨詢
建站咨詢
