python-for其他用法補充
grade_list = [("小雞", 100), ("兩大類", 0), ("小喵", 80), ("小蟲", 60)]
for item in grade_list:
print (item[0], item[1])
print ('---------------------')
# 可以直接改寫
for (name, grade) in grade_list:
print (name, grade)
print ('---------------------')
# 或是這樣
for name, grade in grade_list:
print (name, grade)
print ('---------------------')
num_list = [1, 2, 3]
for num in num_list:
print (num)
num_list.remove(num)
print (num_list)
print ('---------------------')
num_list = [1, 2, 3]
for num in num_list[:]:
print (num)
num_list.remove(num)
print (num_list)
print ('---------------------')
fruits = ['apple', 'banana', 'orange']
for i in fruits:
print(i)
print ('---------------------')
fruits = ['apple', 'banana', 'orange']
for i in range(len(fruits)):
print(fruits[i])
print ('---------------------')
# 反轉
fruits = ['apple', 'banana', 'orange']
for i in reversed(fruits):
print(i)
output如下
D:\>python test.py
小雞 100
兩大類 0
小喵 80
小蟲 60
---------------------
小雞 100
兩大類 0
小喵 80
小蟲 60
---------------------
小雞 100
兩大類 0
小喵 80
小蟲 60
---------------------
1
3
[2]
---------------------
1
2
3
[]
---------------------
apple
banana
orange
---------------------
apple
banana
orange
---------------------
orange
banana
apple
留言
張貼留言