Python 简明教程

Python - Loop Tuples

Loop Through Tuple Items

在 Python 中遍历元组项是指按顺序迭代 tuple 中的每个元素。

Looping through tuple items in Python refers to iterating over each element in a tuple sequentially.

在 Python 中,我们可以通过多种方式遍历 tuple 的项,最常见的是 for loop 。我们还可以使用 while loop 遍历元组项,尽管它需要显式处理循环控制变量,即索引。

In Python we can loop through the items of a tuple in various ways, with the most common being the for loop. We can also use the while loop to iterate through tuple items, although it requires additional handling of the loop control variable explicitly i.e. an index.

Loop Through Tuple Items with For Loop

Python 中的 for 循环用于遍历序列(如列表、元组、字典、字符串或范围)或任何其他可迭代对象。它允许您为序列中的每个项目重复执行一个代码块。

A for loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, string, or range) or any other iterable object. It allows you to execute a block of code repeatedly for each item in the sequence.

for 循环中,你可以使用变量访问序列中的每个项,从而可以根据该项的值执行操作或逻辑。我们可以通过遍历元组中的每个项,使用 for 循环遍历元组项。

In a for loop, you can access each item in a sequence using a variable, allowing you to perform operations or logic based on that item’s value. We can loop through tuple items using for loop by iterating over each item in the tuple.

Syntax

以下是使用 for 循环遍历 Python 中元组项的基本语法 −

Following is the basic syntax to loop through items in a tuple using a for loop in Python −

for item in tuple:
   # Code block to execute

Example

在以下示例中,我们使用 for 循环迭代元组 “tup” 中的每个元素,并检索每个元素,在其后加上一个空格 ―

In the following example, we are using a for loop to iterate through each element in the tuple "tup" and retrieving each element followed by a space on the same line −

tup = (25, 12, 10, -21, 10, 100)
for num in tup:
   print (num, end = ' ')

Output

以下是上面代码的输出: -

Following is the output of the above code −

25 12 10 -21 10 100

Loop Through Tuple Items with While Loop

Python 中的 while 循环用于重复执行一段代码块,只要一个指定的条件计算为“True”。

A while loop in Python is used to repeatedly execute a block of code as long as a specified condition evaluates to "True".

我们可以通过初始化一个索引变量,然后使用索引变量遍历元组并将其递增直到达到元组末尾,使用 while 循环遍历元组项。

We can loop through tuple items using while loop by initializing an index variable, then iterating through the tuple using the index variable and incrementing it until reaching the end of the tuple.

Syntax

以下是使用 while 循环遍历 Python 中元组项的基本语法 −

Following is the basic syntax for looping through items in a tuple using a while loop in Python −

while condition:
   # Code block to execute

Example

在以下示例中,我们使用 while 循环迭代元组 “my_tup” 中的每个项。我们使用一个索引变量 “index” 顺序访问每个项,在每次迭代后对其递增以移动到下一个项 −

In the below example, we iterate through each item in the tuple "my_tup" using a while loop. We use an index variable "index" to access each item sequentially, incrementing it after each iteration to move to the next item −

my_tup = (1, 2, 3, 4, 5)
index = 0

while index < len(my_tup):
   print(my_tup[index])
   index += 1

Output

上述代码的输出如下:

Output of the above code is as follows −

1
2
3
4
5

Loop Through Tuple Items with Index

索引是一个数字值,表示序列(例如元组)中元素的位置,从第一个元素的 0 开始。

An index is a numeric value representing the position of an element within a sequence, such as a tuple, starting from 0 for the first element.

我们可以通过迭代与元组长度相对应的索引范围,并在循环中使用索引访问每个元素,使用索引来遍历元组项。

We can loop through tuple items using index by iterating over a range of indices corresponding to the length of the tuple and accessing each element using the index within the loop.

Example

本示例使用整数初始化元组 “tup”,并创建一个与元组长度相对应的索引范围。然后,它遍历范围内的每个索引,并打印元组 “tup” 中该索引处的值 −

This example initializes a tuple "tup" with integers and creates a range of indices corresponding to the length of the tuple. Then, it iterates over each index in the range and prints the value at that index in the tuple "tup" −

tup = (25, 12, 10, -21, 10, 100)
indices = range(len(tup))
for i in indices:
   print ("tup[{}]: ".format(i), tup[i])

Output

我们得到了如下输出 −

We get the output as shown below −

tup[0]: 25
tup[1]: 12
tup[2]: 10
tup[3]: -21
tup[4]: 10
tup[5]: 100