新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:打印集合中的偶數(shù)
創(chuàng)新互聯(lián)Python教程:

西峰ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
寫一個 Python 程序來打印集合中的偶數(shù)。if 語句(if(val % 2 == 0))測試 Set 項是否可被 2 整除,并返回零。如果為真,則在集合中打印該偶數(shù)。
# Set Even Numbers
evSet = {15, 26, 19, 78, 64, 35, 66}
print("Even Set Items = ", evSet)
print("\nThe Even Numbers in this evSet Set are:")
for val in evSet:
if(val % 2 == 0):
print(val, end = " ")在 Python 集中打印偶數(shù)輸出
Even Set Items = {64, 66, 35, 19, 26, 78, 15}
The Even Numbers in this evSet Set are:
64 66 26 78 這個 Python 集合程序允許輸入集合項目并打印偶數(shù)。
# Set Even Numbers
evSet = set()
number = int(input("Enter the Total Even Set Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Set Item = " %i))
evSet.add(value)
print("Even Set Items = ", evSet)
print("\nThe Even Numbers in this evSet Set are:")
for val in evSet:
if(val % 2 == 0):
print(val, end = " ")
在這個 Python Set 的例子中,我們創(chuàng)建了一個 setEvenNumbers 函數(shù),它打印偶數(shù)。
# Set Even Numbers
def setEvenNumbers(evset):
for val in evSet:
if(val % 2 == 0):
print(val, end = " ")
evSet = set()
number = int(input("Enter the Total Even Set Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Set Item = " %i))
evSet.add(value)
print("Even Set Items = ", evSet)
print("\nThe Even Numbers in this evSet Set are:")
setEvenNumbers(evSet)
Enter the Total Even Set Items = 7
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 = 77
Enter the 7 Set Item = 89
Even Set Items = {33, 66, 44, 77, 22, 55, 89}
The Even Numbers in this evSet Set are:
66 44 22 網(wǎng)頁名稱:Python程序:打印集合中的偶數(shù)
分享地址:http://fisionsoft.com.cn/article/djpiisg.html


咨詢
建站咨詢
