亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Iterate

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 循環(huán)迭代字符串
for char in 'liangdianshui' :
    print ( char , end = ' ' )
print('\n')
# 2、for 循環(huán)迭代 list
list1 = [1,2,3,4,5]
for num1 in list1 :
    print ( num1 , end = ' ' )
print('\n')
# 3、for 循環(huán)也可以迭代 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 c
Continuing Learning
||
submitReset Code