新聞中心
這里有您想知道的互聯網營銷解決方案
Python程序:統(tǒng)計元組中正數和負數
創(chuàng)新互聯Python教程:

成都創(chuàng)新互聯是一家集網站建設,吉林企業(yè)網站建設,吉林品牌網站建設,網站定制,吉林網站建設報價,網絡營銷,網絡優(yōu)化,吉林網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。
編寫一個 Python 程序,使用 for 循環(huán)范圍來計算元組中的正數和負數。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 循環(huán)(對于 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 = 4Python 程序使用 While 循環(huán)計算元組中的正數和負數。
# 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 示例中,我們創(chuàng)建了一個返回正數和負數計數的函數。
# 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程序:統(tǒng)計元組中正數和負數
標題鏈接:http://www.dlmjj.cn/article/cdphejg.html


咨詢
建站咨詢
