在Python中,可以使用for循環遍歷元組。以下是使用for循環遍歷元組的示例代碼:
tuple1 = (1, 2, 3, 4, 5) for item in tuple1: print(item)
這段代碼將會輸出元組中的每個元素:
1 2 3 4 5
在循環中,每次迭代時,將元組中的一個元素賦值給變量item,然后使用print()函數將其打印出來。通過這種方式,可以遍歷元組中的每個元素。
item
print()