name = [1,2,3,4,5,6]
index = 0
print(len(name))
while index < len(name):
element = name[index]
print(f"列表元素:{element}")
index += 1
2)for循环
name = [1,2,3,4,5,6]
for element in name:
print(f"列表元素:{element}")
3)while循环遍历元素和for循环遍历元素的对比
4)总结
5)练习
list = [1,2,3,4,5,6,7,8,9,10]
index = 0
inst2 = []
while index < len(list):
a = list[index]
if a%2 == 0:
inst2.append(a)
index += 1
print(inst2)
list = [1,2,3,4,5,6,7,8,9,10]
index = 0
inst2 = []
for a in list:
if a%2 == 0:
inst2.append(a)
print(inst2)