【bool, and,or 】判断

bool

  • 判断True或是False
    • bool值为0的有  None, 0, 0.0, 空字符串,空空列表, 空元组, 空集合, 空字典 
print(2 and 2)      # 2
print(bool(2 and 2))    # True

and,or

  • and  有假为假
  • or     有真为真
  • and 优先级高于or 
# print(bool(-1))     # True
# print(bool(1))      # True
# print(bool(-0))     # False
# print(bool(object))     # True

# python中False值:None,int中的0,float中的0.0,空字符串,空列表,空元组,空集合set(),空字典{}
# print(bool(None))           # False
# print(bool(0))              # False
# print(bool(0.0))            # False
# print(bool(''))             # False
# print(bool(()))             # False
# print(bool([]))             # False
# print(bool(set()))          # False
# print(bool({}))             # False

# print(bool(' '))            # True
# print(bool(['']))           # True

# print(1 and 5 or 0)     # 5
# print(1 or 5 and 0 or 6 and 0)

 

posted @ 2022-06-10 00:57  淫鬻  阅读(46)  评论(0编辑  收藏  举报