新聞中心
創(chuàng)新互聯(lián)Python教程:

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),吉林企業(yè)網(wǎng)站建設(shè),吉林品牌網(wǎng)站建設(shè),網(wǎng)站定制,吉林網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,吉林網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
編寫一個(gè) Python 程序,使用 for 循環(huán)范圍來計(jì)算元組中的正數(shù)和負(fù)數(shù)。if 條件(if (posngTuple[i] >= 0))檢查元組項(xiàng)是否大于或等于零。如果為真,我們向正元組計(jì)數(shù)添加一個(gè);否則(tNegativeCount = tNegativeCount+1),在負(fù)元組計(jì)數(shù)值上加 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)
計(jì)算元組中正數(shù)和負(fù)數(shù)的 Python 程序
在這個(gè) Python 正負(fù)示例中,我們使用 for 循環(huán)(對(duì)于 posngTuple 中的 pntup)來迭代實(shí)際的元組值,并檢查它們是否大于或等于零。
# 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 = 4Python 程序使用 While 循環(huán)計(jì)算元組中的正數(shù)和負(fù)數(shù)。
# 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在這個(gè) Python Tuple 示例中,我們創(chuàng)建了一個(gè)返回正數(shù)和負(fù)數(shù)計(jì)數(shù)的函數(shù)。
# 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 當(dāng)前標(biāo)題:Python程序:統(tǒng)計(jì)元組中正數(shù)和負(fù)數(shù)
文章位置:http://m.5511xx.com/article/cdphejg.html


咨詢
建站咨詢
