編寫一個 Python 程序,使用 for 循環範圍打印元組中的奇數(對於 i in 範圍(len(oddTuple))。if 語句(if(oddTuple[i] % 2!= 0)檢查每個可被二整除的元組項是否不等於零。如果為真,打印元組中的奇數。
# Tuple Odd Numbers
oddTuple = (9, 22, 33, 45, 56, 77, 89, 90)
print("Odd Tuple Items = ", oddTuple)
print("\nThe Odd Numbers in oddTuple Tuple are:")
for i in range(len(oddTuple)):
if(oddTuple[i] % 2 != 0):
print(oddTuple[i], end = " ")
Odd Tuple Items = (9, 22, 33, 45, 56, 77, 89, 90)
The Odd Numbers in oddTuple Tuple are:
9 33 45 77 89
使用 For 循環打印元組中奇數的 Python 程序
在這個 Python 奇數示例中,我們使用 for 循環(對於 oddTuple 中的 tup)來迭代實際的 Tuple 項以找到奇數。
# Tuple Odd Numbers
oddTuple = (19, 98, 17, 23, 56, 77, 88, 99, 111)
print("Tuple Items = ", oddTuple)
print("\nThe Odd Numbers in this oddTuple Tuple are:")
for tup in oddTuple:
if(tup % 2 != 0):
print(tup, end = " ")
Tuple Items = (19, 98, 17, 23, 56, 77, 88, 99, 111)
The Odd Numbers in this oddTuple Tuple are:
19 17 23 77 99 111
Python 程序使用 While 循環返回元組中的奇數。
# Tuple Odd Numbers
oddTuple = (25, 19, 44, 53, 66, 79, 89, 22, 67)
print("Odd Tuple Items = ", oddTuple)
i = 0
print("\nThe Odd Numbers in oddTuple Tuple are:")
while (i < len(oddTuple)):
if(oddTuple[i] % 2 != 0):
print(oddTuple[i], end = " ")
i = i + 1
Odd Tuple Items = (25, 19, 44, 53, 66, 79, 89, 22, 67)
The Odd Numbers in oddTuple Tuple are:
25 19 53 79 89 67
在這個 Python Tuple 示例中,我們創建了一個函數(tupleednumbers(Odd Tuple))來查找和打印奇數。
# Tuple Odd Numbers
def tupleOddNumbers(oddTuple):
for tup in oddTuple:
if(tup % 2 != 0):
print(tup, end = " ")
oddTuple = (122, 55, 12, 11, 67, 88, 42, 99, 17, 64)
print("Tuple Items = ", oddTuple)
print("\nThe Odd Numbers in oddTuple Tuple are:")
tupleOddNumbers(oddTuple)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/288961.html