寫一個 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://www.506064.com/zh-hant/n/126917.html