Python tutorial: detailed explanation of epoch group in Python

Welcome to stationmaster Online Station master school study Python Knowledge, this paper studies in Python Medium traversal tuple Detailed explanation. The main contents of this knowledge point are: direct use   for loop Traversal tuple , use for Cycle and enumerate () function passes through the epoch group, uses the for loop, and tuple () function passes through the epoch group, uses the for loop, and range () function passes through the epoch group, uses the for loop, and iter () function history group and usage   while Cycle through the epoch group.

catalogue

1. Directly use the for loop to iterate through the epoch group

2. Use the for loop and the enumerate() function to iterate through the epoch group

3. Use the for loop and tuple() function to iterate through the epoch group

4. Use the for loop and the range() function to iterate through the epoch group

5. Use the for loop and the iter() function to iterate through a set of variables

6. Use while to cycle through the epoch group

 

Webmaster Online Warm tips: the content of this tutorial is similar to< Detailed explanation of traversing the list in Python >Generally consistent, you can compare and learn. (basically list Change to tuple, [] to (), list Change to tuple)

Let's first explain the meaning of traversal. It means all-round and everywhere, and calendar means line and travel. The so-called traversal means to travel all over and around.

Traversing tuples is to get data from tuples from beginning to end.

Again, a tuple is an immutable sequence. You can't modify a single element, but you can traverse each element of a tuple.

stay Python In, there are many ways to traverse tuples. Here are some common traversal methods:

1. Directly use the for loop to iterate through the epoch group

Use the for loop directly Traversal list , only the value of the element can be output. The syntax format is as follows:

for variable element in list:
   #output variable element

Columns, as defined Python And then traverse the tuple through the for loop and output each content. code As follows:

print("Python Design concept:")
python = ("grace","to make clear","simple")
for linian in python:
    print(linian)

The results after implementation are as follows:

Python Design concept:
grace
 to make clear
 simple
>>> 

2. Using for loops and enumerate () function epoch group

Using the for loop and enumerate() function, you can output the index value and element content at the same time. Its syntax format is as follows:

for index,Variable element in enumerate(tuple)
    #output index And variable elements

For example, define the design concept of python, then traverse the tuple through the for loop and enumerate() function, and output the index and each content. code As follows:

print("Python Design concept:")
python = ("grace","to make clear","simple")
for index,linian in enumerate(python):
    print(index,linian)

The results are as follows:

Python Design concept:
0 grace
1 to make clear
2 simple
>>> 

3. Use the for loop and tuple() function to iterate through the epoch group

For example, define the design concept of python, then traverse the tuple through the for loop and tuple() function, and output each content. The code is as follows:

print("Python Design concept:")
python = ("grace","to make clear","simple")
for linian in tuple(python):
    print(linian)

The results are as follows:

Python Design concept:
grace
 to make clear
 simple
>>> 

4. Using for loops and range () function epoch group

Define the list of number, then traverse the tuple through the for loop and the range() function, and output each content. The code is as follows:

number = (1122,2366,4400,5577,8888)
for i in range(len(number)):
    print(i,number[i])

The execution results are:

0 1122
1 2366
2 4400
3 5577
4 8888
>>> 

You can output without index. The code is:

number = (1122,2366,4400,5577,8888)
for i in range(len(number)):
    print(number[i])

The operation result is:

1122
2366
4400
5577
8888
>>> 

We put the output results on the same line, and the code is:

number = (1122,2366,4400,5577,8888)
for i in range(len(number)):
    print(number[i],end=" ")

The execution results are:

1122 2366 4400 5577 8888 
>>> 

Note that the range() function can only be used for tuples of numbers, and errors will be reported in non numbers.

5. Use the for loop and the iter() function to iterate through a set of variables

For example, define the design concept of python, then use the for loop and the iter() function to traverse the tuple and output each content. The code is as follows:

print("Python Design concept:")
python = ("grace","to make clear","simple")
for linian in iter(python):
    print(linian)

The output results are as follows:

Python Design concept:
grace
 to make clear
 simple
>>> 

6. Use while Cyclic epoch group

Define the tuple of number, and then traverse the tuple through the while loop and output each content. The code is as follows:

number = (1122,2366,4400,5577,8888)
i = 0
while i < len(number):
    print(number[i])
    i = i + 1

The operation results are as follows:

1122
2366
4400
5577
8888
>>> 

So far, in Python, the methods of directly using the for loop to iterate over the epoch group, using the for loop and enumerate() function to iterate over the epoch group, using the for loop and tuple() function to iterate over the epoch group, using the for loop and range() function to iterate over the epoch group, using the for loop and iter() function to iterate over the epoch group, and using the while loop to iterate over the epoch group have been explained, If you have any questions, you can leave a message!

Tags: Python Back-end

Posted on Wed, 10 Nov 2021 17:27:17 -0500 by Mistah Roth