循环遍历取出偶数
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# while 循环
def while_myfunc():
new_list =[]
index_while = 0
while index_while < len(num_list):
if(num_list[index_while] % 2 == 0):
new_list.append(num_list[index_while])
index_while += 1
print(f"通过while循环,从列表{num_list} 中取出偶数,组成新列表:{new_list}")
#for 循环
def for_myfunc():
new_list_for = []
for x in num_list:
if (x % 2 == 0):
new_list_for.append(x)
print(f"通过for循环,从列表{num_list} 中取出偶数,组成新列表:{new_list_for}")
while_myfunc()
for_myfunc()