What is iteration?
For example, in Java, we traverse the elements in the List collection through the subscripts of the List collection. In Python, given a list or tuple, we can traverse the list or tuple through a for loop. tuple, this kind of traversal is iteration.
However, the abstraction level of Python's for loop is higher than that of Java's for loop. Why do you say this? Because Python's for loop can be used not only on lists or tuples, but also on other iterable objects. In other words, as long as it is an iterable object, it can be iterated regardless of whether it has a subscript or not.
For example:
# -*- coding: UTF-8 -*- # 1、for 循环迭代字符串 for char in 'liangdianshui' : print ( char , end = ' ' ) print('\n') # 2、for 循环迭代 list list1 = [1,2,3,4,5] for num1 in list1 : print ( num1 , end = ' ' ) print('\n') # 3、for 循环也可以迭代 dict (字典) dict1 = {'name':'两点水','age':'23','sex':'男'} for key in dict1 : # 迭代 dict 中的 key print ( key , end = ' ' ) print('\n') for value in dict1.values() : # 迭代 dict 中的 value print ( value , end = ' ' ) print ('\n') # 如果 list 里面一个元素有两个变量,也是很容易迭代的 for x , y in [ (1,'a') , (2,'b') , (3,'c') ] : print ( x , y )
The output result is as follows:
l i a n g d i a n s h u i 1 2 3 4 5 name age sex 两点水 23 男 1 a 2 b 3 cNext Section