例如:字典映射
day = 6
switcher = {
0:'Sunday',
1:'Monday',
2:'Tuesday'
}
day_name = switcher.get(day,'Unknown')
print(day_name)
列表推导式:
None:空 不为空字符串和空的列表
#set也可以被推导
a = [1,2,3,4,5,6,7,8]
b = [i**3 for i in a]
# b= {i**2 for i in a if i>=5}
print(b)
students = {
'张三':18,
'李四':20,
'王五':15
}
b = [key for key,value in students.items()]
for x in b:
print(x)
print(type(None))
None永远对应的是False
class Test():
def __len__(self):
return 0
test = Test()
if test:
print('S')
else:
print('F')
class Test():
def __bool__(self):#是最正宗的方法
return False
def __len__(self):
return True
print(bool(Test()))