非空即真

非空即真,非0即真 ---可以简化自己的代码

None {} () [] ''   这些都是False

非空就是True

username = input('username:') 

 if  username.strip(): #不为空 
     print('你输入的不为空',username)
 else:
     print('username不能为空')
username = input('username:') #
print(not username.strip())

if not username.strip():#
    print('不能为空',username)
else:
    print('你输入是',username)
d = {'a':1,'b':2,'c':3,'d':0}

if d.get('d'):
    print('取到值了')
else:
    print('做另外的操作')

如上就会有bug,d存的值是0的时候,会执行else

原文地址:https://www.cnblogs.com/Noul/p/15020660.html