a = 1b = 2#下面这个c = a if a>1 else b #和下面是一样的效果if a > 1: c = aelse: c = b #打印偶数s=[]nums = list(range(1,11))for i in nums: if i%2==0: s.append(i)#打印偶数print(s) 三元写法:ss = [i for i in nums if i%2==0]print(ss)#这2种是一样的
时间: 2024-11-05 15:57:20
a = 1b = 2#下面这个c = a if a>1 else b #和下面是一样的效果if a > 1: c = aelse: c = b #打印偶数s=[]nums = list(range(1,11))for i in nums: if i%2==0: s.append(i)#打印偶数print(s) 三元写法:ss = [i for i in nums if i%2==0]print(ss)#这2种是一样的