新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python程序:計(jì)算元組項(xiàng)和
創(chuàng)新互聯(lián)Python教程:

編寫一個(gè) Python 程序來查找元組中所有項(xiàng)目的總和。這里,我們使用 Tuple sum 函數(shù)返回所有 Tuple 項(xiàng)的總和。
# Tuple Sum of All Items
numTuple = (20, 40, 65, 75, 80, 220)
print("Tuple Items = ", numTuple)
tupleSum = sum(numTuple)
print("\nThe Sum of numTuple Tuple Items = ", tupleSum)
Tuple Items = (20, 40, 65, 75, 80, 220)
The Sum of numTuple Tuple Items = 500
尋找元組項(xiàng)之和的 Python 程序
在這個(gè) Python 示例中,for 循環(huán)(用于 numTuple 中的 tup)迭代所有元組項(xiàng)。在循環(huán)(tupleSum = tupleSum + tup)中,我們將每個(gè)元組項(xiàng)添加到 tupleSum 中,并打印相同的內(nèi)容。
# Tuple Sum of All Items
numTuple = (11, 22, 33, 44, 55, 66, 77, 88, 99)
print("Odd Tuple Items = ", numTuple)
tupleSum = 0
for tup in numTuple:
tupleSum = tupleSum + tup
print("\nThe Sum of numTuple Tuple Items = ", tupleSum)
Odd Tuple Items = (11, 22, 33, 44, 55, 66, 77, 88, 99)
The Sum of numTuple Tuple Items = 495Python 程序使用 While 循環(huán)計(jì)算所有元組項(xiàng)的總和。
# Tuple Sum of All Items
numTuple = (25, 45, 65, 75, 95, 125, 256, 725)
print("Odd Tuple Items = ", numTuple)
tupleSum = 0
i = 0
while (i < len(numTuple)):
tupleSum = tupleSum + numTuple[i]
i = i + 1
print("\nThe Sum of numTuple Tuple Items = ", tupleSum)
Odd Tuple Items = (25, 45, 65, 75, 95, 125, 256, 725)
The Sum of numTuple Tuple Items = 1411在這個(gè) Python Tuple 示例中,我們創(chuàng)建了一個(gè) sumOfTupleItems(numTuple)函數(shù),該函數(shù)將計(jì)算并返回所有 tuple 項(xiàng)的總和。
# Tuple Sum
def sumOfTupleItems(numTuple):
tupleSum = 0
for tup in numTuple:
tupleSum = tupleSum + tup
return tupleSum
numTuple = (16, 31, 24, 98, 123, 78, 56, 67, 22)
print("Tuple Items = ", numTuple)
tSum = sumOfTupleItems(numTuple)
print("\nThe Sum of numTuple Tuple Items = ", tSum) 文章題目:Python程序:計(jì)算元組項(xiàng)和
文章位置:http://www.dlmjj.cn/article/dhpiged.html


咨詢
建站咨詢
