一、For...in
1. 數字迴圈
for i in range(10):
print(i)
2. 陣列迴圈
persons = ['A1', 'A2', 'A3', 'A4']
for i in persons:
print(i)
3. 數字迴圈與陣列迴圈特別寫法
(1)
squares = [i for i in range(10)]
print(squares)
squares2 = [i for i in range(10) if i>4]
print(squares2)
(2)
persons = ['A1', 'A2', 'A3', 'A4']
nums=[i for i in persons]
4. for 指名陣列位置
T=["A","B","C","D","E"]
for i in T[2]:
print(i)
3. enumerate() 函式顯示索引跟值
persons=["A","B","C","D","E"]
for i, person in enumerate(persons):
print(f'{i}值 {person}', end='')
二、While
1. while 迴圈用法
i = 0
while i < 100:
print(i)
i = i + 1
2. while 陣列輸出
persons = ['A1', 'A2', 'A3', 'A4']
while 0 < len(persons):
print(persons.pop())
沒有留言:
張貼留言
影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。