Python程序:打印元组中正数

创新互联Python教程:

为溆浦等地区用户提供了全套网页设计制作服务,及溆浦网站建设行业解决方案。主营业务为成都网站制作、成都网站设计、外贸营销网站建设、溆浦网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

编写一个 Python 程序,使用 for 循环范围打印元组中的正数。if 语句(if(posTuple[i] >= 0))检查元组项是否大于或等于零。如果为真,则打印正元组编号。

# Tuple Positive Numbers

posTuple = (3, -8, 6, 4, -88, -11, 13, 19)
print("Positive Tuple Items = ", posTuple)

print("\nThe Positive Numbers in posTuple Tuple are:")
for i in range(len(posTuple)):
    if(posTuple[i] >= 0):
        print(posTuple[i], end = "  ")
Positive Tuple Items =  (3, -8, 6, 4, -88, -11, 13, 19)

The Positive Numbers in posTuple Tuple are:
3  6  4  13  19 

使用 For 循环打印元组中正数的 Python 程序。

在这个 Python 正数示例中,我们使用 for 循环(用于后置元组中的 potup)来迭代实际的元组后置项。

# Tuple Positive Numbers

posTuple = (25, 32, -14, 17, -6, -1, 98, 11, -9, 0)
print("Positive Tuple Items = ", posTuple)

print("\nThe Positive Numbers in this posTuple Tuple are:")
for potup in posTuple:
    if(potup >= 0):
        print(potup, end = "  ")
Positive Tuple Items =  (25, 32, -14, 17, -6, -1, 98, 11, -9, 0)

The Positive Numbers in this posTuple Tuple are:
25  32  17  98  11  0 

Python 程序使用 While 循环返回或显示元组中的正数。

# Tuple Positive Numbers

posTuple = (25, -11, -22, 17, 0, -9, 43, 17, -10) 
print("Positive Tuple Items = ", posTuple)

i = 0

print("\nThe Positive Numbers in posTuple Tuple are:")
while (i < len(posTuple)):
    if(posTuple[i] >= 0):
        print(posTuple[i], end = "  ")
    i = i + 1
Positive Tuple Items =  (25, -11, -22, 17, 0, -9, 43, 17, -10)

The Positive Numbers in posTuple Tuple are:
25  17  0  43  17 

在这个 Python Tuple 示例中,我们创建了一个函数(tuplepossivennumbers(posTuple))来查找和打印正数。

# Tuple Positive Numbers

def tuplePositiveNumbers(posTuple):
    for potup in posTuple:
        if(potup >= 0):
            print(potup, end = "  ")

posTuple = (19, -32, -17, 98, 44, -12, 0, 78, -2, 4, -5) 
print("Positive Tuple Items = ", posTuple)

print("\nThe Positive Numbers in this posTuple Tuple are:")
tuplePositiveNumbers(posTuple)

本文标题:Python程序:打印元组中正数
分享链接:http://www.shufengxianlan.com/qtweb/news39/284739.html

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

广告

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