您的位置:

Python 程序:计算列表中偶数和奇数的和

写一个 Python 程序,使用 For 循环、While 循环和函数,用一个实际例子来求列表中偶数和奇数的和。

Python 程序使用 For 循环查找列表中偶数和奇数的和

在这个 python 程序中,我们使用 For 循环来迭代给定列表中的每个元素。在 Python 循环中,我们使用 If 语句来检查和查找偶数和奇数的和。

# 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 程序中,为了在一个列表中找到偶数和奇数的和,用户输入了项= [2,3,4,5],偶数 和= 0,奇数 和= 0。

对于循环–第一次迭代:对于范围(0,4) 中的 0,条件为真。所以,进入 If 语句

if(NumList[0]% 2 = = 0)= > if(2% 2 = = 0)–条件为真 偶数 Sum =偶数 Sum+NumList[0]=>0+2 = 2

第二次迭代:对于范围(0,4)中的 1–条件为真 如果(NumList[1] % 2 == 0) = >如果(3% 2 = = 0)–条件为假,则进入 Else 块。 奇 和=奇 和+ NumList[1] = > 0 + 3 = 3

第三次迭代:对于范围(0,4)中的 2–条件为真 如果(NumList[2] % 2 == 0) = >如果(4% 2 = = 0)–条件为真 偶数 _Sum = 2 + 4 = 6

第四次迭代:对于范围(0,4)中的 3–条件为真 如果(5% 2 = = 0)–条件为假,则进入否则块。 奇数 _ 总和= 3 + 5 = 8

第五次迭代:对于范围(4)中的 4–条件为假。因此, Python 退出 For 循环

Python 程序使用 While 循环查找列表中偶数和奇数的和

这个计算偶数和奇数之和的 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 循环输出的 Python 列表中偶数和奇数的总和

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

用函数计算列表中偶数和奇数之和的 Python 程序

这个 Python 奇数和偶数的和列表程序与第一个示例相同。但是,我们使用函数来分离逻辑

# 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)

使用函数输出的列表中偶数和奇数的总和

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