Python程序:统计元组中正数和负数

创新互联Python教程:

成都创新互联是一家集网站建设,吉林企业网站建设,吉林品牌网站建设,网站定制,吉林网站建设报价,网络营销,网络优化,吉林网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

编写一个 Python 程序,使用 for 循环范围来计算元组中的正数和负数。if 条件(if (posngTuple[i] >= 0))检查元组项是否大于或等于零。如果为真,我们向正元组计数添加一个;否则(tNegativeCount = tNegativeCount+1),在负元组计数值上加 1。

# Count Positive and Negative Numbers

posngTuple = (3, -22, -44, 19, -99, -37, 4, 11, -89)
print("Positive and Negative Tuple Items = ", posngTuple)

tPositiveCount = tNegativeCount = 0

for i in range(len(posngTuple)):
    if (posngTuple[i] >= 0):
        tPositiveCount = tPositiveCount + 1
    else:
        tNegativeCount = tNegativeCount + 1

print("The Count of Positive Numbers in posngTuple = ", tPositiveCount)
print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)

计算元组中正数和负数的 Python 程序

在这个 Python 正负示例中,我们使用 for 循环(对于 posngTuple 中的 pntup)来迭代实际的元组值,并检查它们是否大于或等于零。

# Count Positive and Negative Numbers

posngTuple = (55, -99, -88, 0, -78, 22, 4, -66, 21, 33)
print("Positive and Negative Tuple Items = ", posngTuple)

tPositiveCount = tNegativeCount = 0
for pntup in posngTuple:
    if(pntup >= 0):
        tPositiveCount = tPositiveCount + 1
    else:
        tNegativeCount = tNegativeCount + 1

print("The Count of Positive Numbers in posngTuple = ", tPositiveCount)
print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)
Positive and Negative Tuple Items =  (55, -99, -88, 0, -78, 22, 4, -66, 21, 33)
The Count of Positive Numbers in posngTuple =  6
The Count of Negative Numbers in posngTuple =  4

Python 程序使用 While 循环计算元组中的正数和负数。

# Count of Tuple Positive and Negative Numbers

posngTuple = (11, -22, -33, 44, 55, -66, -77, 0, -99)
print("Positive and Negative Tuple Items = ", posngTuple)

tPositiveCount = tNegativeCount = 0
i = 0

while (i < len(posngTuple)):
    if(posngTuple[i] >= 0):
        tPositiveCount = tPositiveCount + 1
    else:
        tNegativeCount = tNegativeCount + 1
    i = i + 1

print("The Count of Positive Numbers in posngTuple = ", tPositiveCount)
print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)
Positive and Negative Tuple Items =  (11, -22, -33, 44, 55, -66, -77, 0, -99)
The Count of Positive Numbers in posngTuple =  4
The Count of Negative Numbers in posngTuple =  5

在这个 Python Tuple 示例中,我们创建了一个返回正数和负数计数的函数。

# Count of Tuple Positive and Negative Numbers

def CountOfPositiveNegativeNumbers(evodTuple):
    tPositiveCount = tNegativeCount = 0
    for pntup in evodTuple:
        if(pntup >= 0):
            tPositiveCount = tPositiveCount + 1
        else:
            tNegativeCount = tNegativeCount + 1
    return tPositiveCount, tNegativeCount

evodTuple = (26, -77, -99, 75, 14, -56, 19, 81, -1, 33) 
print("Positive and Negative Tuple Items = ", evodTuple)

PositiveCount, NegativeCount = CountOfPositiveNegativeNumbers(evodTuple)
print("The Count of Positive Numbers in evodTuple = ", PositiveCount)
print("The Count of Negative Numbers in evodTuple = ", NegativeCount)
Positive and Negative Tuple Items =  (26, -77, -99, 75, 14, -56, 19, 81, -1, 33)
The Count of Positive Numbers in evodTuple =  6
The Count of Negative Numbers in evodTuple =  4

新闻标题:Python程序:统计元组中正数和负数
文章起源:http://www.stwzsj.com/qtweb/news9/17009.html

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

广告

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