# This is a learning note for the Liaoche teacher Python tutorial
1. Overview
Given a list or tuple, we can traverse the list or tuple through a for loop, which we call iteration (iteration).
of course, not only list and tuple can iterate. as long as it is an iterative object, there can be iterations, whether or not the subscript is available. including Dict, str, generator
in Python, an iteration is done through a for ... in. .
1.1, the iteration of the dictionary
# define a dictionary
D = {' A ': 1, ' B ': 2, ' C ': 3}
Key of an iterative dictionary
For key in D:
The value of the iteration dictionary
For value in D.values ():
Iterative key-value pairs
For K, V in d.items ()
1.2. String iteration
For ch in ' ABC ':
1.3. List Iteration
# define a list
l=[1,2,3]
Subscript of the Iteration list
For I in L:
Print (l.index (i))
The value of the iteration list
For in L:
Print (i)
The index of the iteration list and the element itself
For I, value in enumerate (L): # Enumerate function can turn a list into an index-element pair C7>for i in L:
Print (L.index (i), i)
Two iterations of a variable
For x, y in [(1, 1), (2, 4), (3, 9)]:
2. Examples
Write a function that uses iterations to find the minimum and maximum values in a list and return a tuple:
#-*-Coding:utf-8-*-
def findminandmax (L):
If Len (L) ==0:
Return (none, none)
MIN=L[0]
MAX=L[0]
For value in L:
If value > Max:
Max=value
Elif Value < min:
Min=value
Return Min,max
Python Learning Note __3.2 iteration