l=[1,2,3,4,5,6]
m1=[]
m2=[]
m3=[]
m4=[]
m5=[]
for i in l:
if i%2==0:
m1.append(i) #如果i对2取余数为0则加入列表
if not i%2:
m2.append(i) #举例:1对2取余为1,not 1则为0,表示False,不添加;
#2对2取余数为0,not 0则为1,表示True,添加
if i%2 is None: #i对2余数为空值则添加
m3.append(i)
if i%2 is not None: #i对2余数不为空值则添加
m4.append(i)
if not i%2 is None: #i对2余数不为空值则添加
m5.append(i)
print(m1)
print(m2)
print(m3)
print(m4)
print(m5)
输出结果:
[2, 4, 6]
[2, 4, 6]
[]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]