編寫一個 Python 程序來打印元組中所有項目的列表。我們可以使用 print 函數打印整個元組。在這個例子中,我們聲明了字符串和 int 元組並打印它們。
# Print Tuple Items
numTuple = (10, 20, 30, 40, 50)
print("The Tuple Items are ")
print(numTuple )
strTuple = ('C#', 'Java', 'Python', 'C')
print("\nThe String Tuple Items are ")
print(strTuple )
在這個 Python 程序中,我們使用 for 循環範圍來訪問每個元組項。第一個 for 循環從第一個元組項迭代到最後一個,並打印每個元素。第二個循環打印字符串元組中的所有水果。
# Print Tuple Items
numTuple = (10, 20, 30, 40, 50)
print("The Tuple Items are ")
for i in range(len(numTuple)):
print("Tuple Item at %d Position = %d" %(i, numTuple[i]))
print("=========")
fruitsTuple = ('apple', 'orange', 'kiwi', 'grape')
for fruit in fruitsTuple:
print(fruit)
The Tuple Items are
Tuple Item at 0 Position = 10
Tuple Item at 1 Position = 20
Tuple Item at 2 Position = 30
Tuple Item at 3 Position = 40
Tuple Item at 4 Position = 50
=========
apple
orange
kiwi
grape
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/128633.html