您的位置:

Python 程序:打印元组项目

编写一个 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