If a list or tuple is given, we can traverse the list or tuple through a for loop. This traversal is called iteration.
In Python, iteration is done through for...in, while in many languages ??such as C or Java, list iteration is done through subscripts, such as Java code:
for (i=0; i } It can be seen that Python’s for loop has a higher level of abstraction For loops in Java, because Python's for loops can not only be used on lists or tuples, but also on other iterable objects. Although the data type list has subscripts, many other data types do not have subscripts. However, as long as it is an iterable object, it can be iterated regardless of whether it has a subscript. For example, dict can be iterated : >>> d = {'a': 1, 'b': 2, 'c': 3}>>> for key in d:... print(key) ... a c b Because dict is not stored in the order of a list, Therefore, the order of the iterated results is likely to be different. By default, dict iterates over keys. If you want to iterate value, you can use for value in d.values(). If you want to iterate key and value at the same time, you can use for k, v in d.items(). Since strings are also iterable objects, they can also be used in for loops: >>> for ch in 'ABC':... print(ch) p> ... A B C So, when we use a for loop, as long as it works For an iterable object, the for loop can run normally, and we don't care whether the object is a list or another data type. This website has many Python systems and basic tutorials, you can check them out. Web link