Python程序:打印集合中的偶数

创新互联Python教程:

西峰ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

写一个 Python 程序来打印集合中的偶数。if 语句(if(val % 2 == 0))测试 Set 项是否可被 2 整除,并返回零。如果为真,则在集合中打印该偶数。

# 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 集中打印偶数输出

Even Set Items =  {64, 66, 35, 19, 26, 78, 15}

The Even Numbers in this evSet Set are:
64  66  26  78 

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

# 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 的例子中,我们创建了一个 setEvenNumbers 函数,它打印偶数。

# 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 

文章名称:Python程序:打印集合中的偶数
网页网址:http://www.36103.cn/qtweb/news35/11635.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联