while語句結構(for循環)
python for 循環可以遍歷任何序列的項目,如一個列表或一個字符串
for循環的一般形式
for 條件判斷 in 一個序列: 執行語句 else: 執行語句
可以使用break語句跳出當前循環體
student=['zhao','qian','sun','li','zhou','wu','zheng','wang'] a='li' for a in student: print('li在student中') break else: print('li不在student中') #運行結果 li在student中
student=['zhao','qian','sun','li','zhou','wu','zheng','wang'] for student in student: print('循環數據:'+student) else: print('循環結束') #運行結果 循環數據:zhao 循環數據:qian 循環數據:sun 循環數據:li 循環數據:zhou 循環數據:wu 循環數據:zheng 循環數據:wang 循環結束