定义一个列表,内容是:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
遍历列表,取出列表内的偶数,并存入一个新的列表对象中
使用while循环和for循环各操作一次
提示:
通过if判断来确认偶数
通过列表的append方法,来增加元素
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# while循环
index = 0
list2 = []
while index < len(list1):
if list1[index] % 2 == 0:
list2.append(list1[index])
index += 1
print(list2)
list2.clear()
# for循环
for element in list1:
if element % 2 == 0:
list2.append(element)
print(list2)