您的位置:首页 > 编程语言 > Python开发

python的逻辑运算符

2018-03-22 16:38 204 查看
python中的逻辑运算符包括 and / or / not 
由于python中的任何数据类型都有逻辑值,所以逻辑运算符可以对所有数据进行操作。下表是不同类型数据的布尔值。

数据类型FalseTrue
整型
0其他
浮点型0.0其他
字符串‘’其他
字典{}其他
元组()其他
列表[]其他
NoneNone 
在python中,逻辑运算返回的值并不限定于True / False对于and的逻辑运算规则:

对于or的逻辑运算规则:



对于not的运算规则:


不同逻辑运算符的优先级:not > and > or附代码:str1 = 'python'
str2 = ''
list1 = ['python']
list2 = []
print(str1 and list1)
print(str2 and list2)
print(str1 and str2)
print(str1 or list1)
print(str2 or list2)
print(str1 or str2)
print(str1 or str2 and list1 or list2) 

#result:

#['python']
#
#
#python
#[]
#python
#python
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息