写一个 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