RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Python程序:打印集合中负数

创新互联python教程:

创新互联公司秉承实现全网价值营销的理念,以专业定制企业官网,成都网站设计、做网站、成都外贸网站建设公司微信小程序开发,网页设计制作,成都手机网站制作营销型网站帮助传统企业实现“互联网+”转型升级专业定制企业官网,公司注重人才、技术和管理,汇聚了一批优秀的互联网技术人才,对客户都以感恩的心态奉献自己的专业和所长。

写一个 Python 程序来打印集合中的负数。for 循环中的 if 语句(if(negaVal < 0))检查集合项是否小于零。如果为真,则打印负集数。

# Set Negative Numbers

NegativeSet = {6, -22, -33, 78, -88, 15, -55, -66, 17}
print("Negative Set Items = ", NegativeSet)

print("\nThe Negative Numbers in this NegativeSet Set are:")
for negaVal in NegativeSet:
    if(negaVal < 0):
        print(negaVal, end = "  ")

在 Python 集中打印负数输出

Negative Set Items =  {6, -88, -55, -22, 78, 15, 17, -66, -33}

The Negative Numbers in this NegativeSet Set are:
-88  -55  -22  -66  -33 

这个 Python 程序允许您输入集合项目并在集合中打印负数。

# Set Negative Numbers

negativeSet = set()

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

print("Negative Set Items = ", negativeSet)

print("\nThe Negative Numbers in this negativeSet Set are:")
for negaVal in negativeSet:
    if(negaVal < 0):
        print(negaVal, end = "  ")

在这个 Python Set 示例中,我们创建了一个 setnegativeNumbers 函数,它可以查找并打印负数。

# Tuple Negative Numbers

def setnegativeNumbers(negativeSet):
    for negVal in negativeSet:
        if(negVal < 0):
            print(negVal, end = "  ")

negativeSet = set()

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

print("Negative Set Items = ", negativeSet)

print("\nThe Negative Numbers in this negativeSet Set are:")
setnegativeNumbers(negativeSet)
Enter the Total Negative Set Items = 5
Enter the 1 Set Item = 22
Enter the 2 Set Item = -33
Enter the 3 Set Item = -87
Enter the 4 Set Item = 58
Enter the 5 Set Item = -71
Negative Set Items =  {-87, 22, -71, 58, -33}

The Negative Numbers in this negativeSet Set are:
-87  -71  -33 

本文名称:Python程序:打印集合中负数
转载注明:http://www.jxjierui.cn/article/dhssggs.html