python中的逻辑运算符包括 and / or / not
由于python中的任何数据类型都有逻辑值,所以逻辑运算符可以对所有数据进行操作。下表是不同类型数据的布尔值。
| 数据类型 | False | True |
|
整型 | 0 | 其他 |
| 浮点型 | 0.0 | 其他 |
| 字符串 | ‘’ | 其他 |
| 字典 | {} | 其他 |
| 元组 | () | 其他 |
| 列表 | [] | 其他 |
| None | None |
在python中,逻辑运算返回的值并不限定于True / False
对于and的逻辑运算规则:
对于or的逻辑运算规则:

不同逻辑运算符的优先级:
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
759

被折叠的 条评论
为什么被折叠?



