Python 程序:统计集合中的偶数和奇数

写一个 Python 程序来计算集合中的偶数和奇数。for 循环(针对 evodSet 中的 eoVal)迭代所有集合项。而 if 条件(if(eoVal % 2 == 0))检查可被二整除的 Set 项是否等于零。如果为真,我们在偶数集合计数上加一;否则(sOddCount = sOddCount + 1),给奇数集计数值加 1。

# Count of Set Even and Odd Numbers

evodSet = {78, 11, 54, 95, 16, 36, 61, 77, 150, 122}
print("Even and Odd Set Items = ", evodSet)

sEvenCount = sOddCount = 0

for eoVal in evodSet:
    if(eoVal % 2 == 0):
        sEvenCount = sEvenCount + 1
    else:
        sOddCount = sOddCount + 1

print("The Count of Even Numbers in evodSet = ", sEvenCount)
print("The Count of Odd  Numbers in evodSet = ", sOddCount)

这个 Python 偶数和奇数示例允许使用 for 循环范围输入集合项。

# Count of Set Even and Odd Numbers

evodSet = set()

number = int(input("Enter the Total Even Odd Set Items = "))
for i in range(1, number + 1):
    value = int(input("Enter the %d Set Item = " %i))
    evodSet.add(value)

print("Even and Odd Set Items = ", evodSet)

sEvenCount = sOddCount = 0

for eoVal in evodSet:
    if(eoVal % 2 == 0):
        sEvenCount = sEvenCount + 1
    else:
        sOddCount = sOddCount + 1

print("The Count of Even Numbers in evodSet = ", sEvenCount)
print("The Count of Odd  Numbers in evodSet = ", sOddCount)

Python 计数偶数和奇数集合输出

Enter the Total Even Odd Set Items = 4
Enter the 1 Set Item = 22
Enter the 2 Set Item = 9
Enter the 3 Set Item = 32
Enter the 4 Set Item = 78
Even and Odd Set Items =  {32, 9, 22, 78}
The Count of Even Numbers in evodSet =  3
The Count of Odd  Numbers in evodSet =  1

在这个 Python 集示例中,我们创建了一个 CountOfSetEvenandOddNumbers 函数,该函数返回偶数和奇数计数。

# Count of Set Even and Odd Numbers

def CountOfSetEvenandOddNumbers(evodSet):
    sEvenCount = sOddCount = 0

    for eoVal in evodSet:
        if(eoVal % 2 == 0):
            sEvenCount = sEvenCount + 1
        else:
            sOddCount = sOddCount + 1
    return sEvenCount, sOddCount

evodSet = set()

number = int(input("Enter the Total Even Odd Set Items = "))
for i in range(1, number + 1):
    value = int(input("Enter the %d Set Item = " %i))
    evodSet.add(value)

print("Even and Odd Set Items = ", evodSet)

sECount, sOCount = CountOfSetEvenandOddNumbers(evodSet)
print("The Count of Even Numbers in evodSet = ", sECount)
print("The Count of Odd  Numbers in evodSet = ", sOCount)

Python 统计集合输出中的偶数和奇数

Enter the Total Even Odd Set Items = 6
Enter the 1 Set Item = 22
Enter the 2 Set Item = 33
Enter the 3 Set Item = 44
Enter the 4 Set Item = 55
Enter the 5 Set Item = 66
Enter the 6 Set Item = 88
Even and Odd Set Items =  {33, 66, 44, 22, 55, 88}
The Count of Even Numbers in evodSet =  4
The Count of Odd  Numbers in evodSet =  2

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
B6UHZ的头像B6UHZ
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相关推荐

  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python列表中负数的个数

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

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • 运维Python和GO应用实践指南

    本文将从多个角度详细阐述运维Python和GO的实际应用,包括监控、管理、自动化、部署、持续集成等方面。 一、监控 运维中的监控是保证系统稳定性的重要手段。Python和GO都有强…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

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

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

    编程 2025-04-29
  • Python中new和init的区别

    new和init都是Python中常用的魔法方法,它们分别负责对象的创建和初始化,本文将从多个角度详细阐述它们的区别。 一、创建对象 new方法是用来创建一个对象的,它是一个类级别…

    编程 2025-04-29
  • Python中capitalize函数的使用

    在Python的字符串操作中,capitalize函数常常被用到,这个函数可以使字符串中的第一个单词首字母大写,其余字母小写。在本文中,我们将从以下几个方面对capitalize函…

    编程 2025-04-29

发表回复

登录后才能评论