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

萍鄉(xiāng)網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
編寫一個 Python 程序,使用 for 循環(huán)范圍來計算元組中的偶數(shù)和奇數(shù)(對于 i in 范圍(len(evodTuple))。if 條件(if(evodTuple[i] % 2 == 0))檢查 Tuple 項是否能被 2 整除。如果為真,我們給偶數(shù)元組計數(shù)加一;否則,給奇數(shù)元組計數(shù)值加一。
# Count of Tuple Even and Odd Numbers
evodTuple = (2, 33, 45, 88, 77, 98, 54, 0, 17)
print("Even and Odd Tuple Items = ", evodTuple)
tEvenCount = tOddCount = 0
for i in range(len(evodTuple)):
if(evodTuple[i] % 2 == 0):
tEvenCount = tEvenCount + 1
else:
tOddCount = tOddCount + 1
print("The Count of Even Numbers in evodTuple = ", tEvenCount)
print("The Count of Odd Numbers in evodTuple = ", tOddCount)
計算元組中偶數(shù)和奇數(shù)的 Python 程序
在這個 Python 示例中,我們使用 for 循環(huán)(用于 evodTuple 中的 oetup)來迭代實際的元組值,并檢查它們是否能被等于零的 2 整除。
# Count of Tuple Even and Odd Numbers
evodTuple = (11, 34, 17, 44, 66, 19, 89, 64, 90)
print("Even and Odd Tuple Items = ", evodTuple)
tEvenCount = tOddCount = 0
for oetup in evodTuple:
if(oetup % 2 == 0):
tEvenCount = tEvenCount + 1
else:
tOddCount = tOddCount + 1
print("The Count of Even Numbers in evodTuple = ", tEvenCount)
print("The Count of Odd Numbers in evodTuple = ", tOddCount)
Even and Odd Tuple Items = (11, 34, 17, 44, 66, 19, 89, 64, 90)
The Count of Even Numbers in evodTuple = 5
The Count of Odd Numbers in evodTuple = 4使用 While 循環(huán)計算元組偶數(shù)和奇數(shù)的 Python 程序。
# Count of Tuple Even and Odd Numbers
evodTuple = (64, 45, 12, 15, 68, 55, 37, 25, 120, 205)
print("Even and Odd Tuple Items = ", evodTuple)
tEvenCount = tOddCount = 0
i = 0
while (i < len(evodTuple)):
if(evodTuple[i] % 2 == 0):
tEvenCount = tEvenCount + 1
else:
tOddCount = tOddCount + 1
i = i + 1
print("The Count of Even Numbers in evodTuple = ", tEvenCount)
print("The Count of Odd Numbers in evodTuple = ", tOddCount)
Even and Odd Tuple Items = (64, 45, 12, 15, 68, 55, 37, 25, 120, 205)
The Count of Even Numbers in evodTuple = 4
The Count of Odd Numbers in evodTuple = 6在這個 Python 元組的例子中,我們創(chuàng)建了一個 CountOfEvenOddOddNumbers 函數(shù),返回偶數(shù)和奇數(shù)的計數(shù)。
# Count of Tuple Even and Odd Numbers
def CountOfEvenOddOddNumbers(evodTuple):
tEvenCount = tOddCount = 0
for oetup in evodTuple:
if(oetup % 2 == 0):
tEvenCount = tEvenCount + 1
else:
tOddCount = tOddCount + 1
return tEvenCount, tOddCount
evodTuple = (12, 26, 77, 99, 66, 75, 14, 256, 19, 81, 11, 33)
print("Even and Odd Tuple Items = ", evodTuple)
evenCount, oddCount = CountOfEvenOddOddNumbers(evodTuple)
print("The Count of Even Numbers in evodTuple = ", evenCount)
print("The Count of Odd Numbers in evodTuple = ", oddCount)
Even and Odd Tuple Items = (12, 26, 77, 99, 66, 75, 14, 256, 19, 81, 11, 33)
The Count of Even Numbers in evodTuple = 5
The Count of Odd Numbers in evodTuple = 7 名稱欄目:Python程序:統(tǒng)計元組中偶數(shù)和奇數(shù)
當(dāng)前路徑:http://www.dlmjj.cn/article/dpsdoej.html


咨詢
建站咨詢
