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

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/295404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-26 17:15
下一篇 2024-12-27 12:56

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Python定义函数判断奇偶数

    本文将从多个方面详细阐述Python定义函数判断奇偶数的方法,并提供完整的代码示例。 一、初步了解Python函数 在介绍Python如何定义函数判断奇偶数之前,我们先来了解一下P…

    编程 2025-04-29
  • Python购物车程序

    Python购物车程序是一款基于Python编程语言开发的程序,可以实现购物车的相关功能,包括商品的添加、购买、删除、统计等。 一、添加商品 添加商品是购物车程序的基础功能之一,用…

    编程 2025-04-29
  • 爬虫是一种程序

    爬虫是一种程序,用于自动获取互联网上的信息。本文将从如下多个方面对爬虫的意义、运行方式、应用场景和技术要点等进行详细的阐述。 一、爬虫的意义 1、获取信息:爬虫可以自动获取互联网上…

    编程 2025-04-29
  • Vb运行程序的三种方法

    VB是一种非常实用的编程工具,它可以被用于开发各种不同的应用程序,从简单的计算器到更复杂的商业软件。在VB中,有许多不同的方法可以运行程序,包括编译器、发布程序以及命令行。在本文中…

    编程 2025-04-29
  • Python一元二次方程求解程序

    本文将详细阐述Python一元二次方程求解程序的相关知识,为读者提供全面的程序设计思路和操作方法。 一、方程求解 首先,我们需要了解一元二次方程的求解方法。一元二次方程可以写作: …

    编程 2025-04-29
  • Python列表中大于某数的元素处理方法

    本文将会介绍如何在Python列表中找到大于某数的元素,并对其进行进一步的处理。 一、查找大于某数的元素 要查找Python列表中大于某数的元素,可以使用列表推导式进行处理。 nu…

    编程 2025-04-29

发表回复

登录后才能评论