当前分类:300例题>>正文

Python示例:打印集合中的奇数

来源:互联网   更新时间:2023年6月24日  

Python 示例

写一个 Python示例来打印集合中的奇数。if 语句(if(odval % 2!= 0))检查可被二整除的集合项是否不等于零。如果为真,则打印集合中的奇数。

# Set Odd Numbers

oddSet = {26, 33, 19, 8, 41, 35, 12, 17}
print("Odd Set Items = ", oddSet)

print("\nThe Odd Numbers in this oddSet Set are:")
for odval in oddSet:
    if(odval % 2 != 0):
        print(odval, end = "  ")

在 python 集中打印奇数输出

Odd Set Items =  {33, 35, 8, 41, 12, 17, 19, 26}

The Odd Numbers in this oddSet Set are:
33  35  41  17  19 

这个 Python 集合程序允许输入集合项目和打印奇数。

# Set Odd Numbers

oddSet = set()

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

print("Odd Set Items = ", oddSet)

print("\nThe Odd Numbers in this oddSet Set are:")
for odval in oddSet:
    if(odval % 2 != 0):
        print(odval, end = "  ")

在这个 Python Set 的例子中,我们创建了一个(setddnumbers(oddSet))函数来打印奇数。

# Tuple Odd Numbers

def setOddNumbers(oddSet):
    for odval in oddSet:
        if(odval % 2 != 0):
            print(odval, end = "  ")

oddSet = set()

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

print("Odd Set Items = ", oddSet)

print("\nThe Odd Numbers in this oddSet Set are:")
setOddNumbers(oddSet)

Python 打印奇数集物品输出

Enter the Total Odd Set Items = 5
Enter the 1 Set Item = 12
Enter the 2 Set Item = 99
Enter the 3 Set Item = 121
Enter the 4 Set Item = 88
Enter the 5 Set Item = 77
Odd Set Items =  {99, 12, 77, 88, 121}

The Odd Numbers in this oddSet Set are:
99  77  121 
本文固定链接:https://6yhj.com/leku-p-4452.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: 积分兑换