python的逻辑运算符
2018-03-22 16:38
204 查看
python中的逻辑运算符包括 and / or / not
由于python中的任何数据类型都有逻辑值,所以逻辑运算符可以对所有数据进行操作。下表是不同类型数据的布尔值。
在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
由于python中的任何数据类型都有逻辑值,所以逻辑运算符可以对所有数据进行操作。下表是不同类型数据的布尔值。
数据类型 | False | True |
整型 | 0 | 其他 |
浮点型 | 0.0 | 其他 |
字符串 | ‘’ | 其他 |
字典 | {} | 其他 |
元组 | () | 其他 |
列表 | [] | 其他 |
None | None |
对于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
相关文章推荐
- python输入以及比较逻辑运算符介绍(含笔记)
- python 位运算符与逻辑运算符(字符串的逻辑运算)
- 总结Python中逻辑运算符的使用
- python逻辑运算符的规律
- python 笔记之 逻辑运算符与数据类型
- Python中的逻辑运算符‘and’、‘or’和‘not’
- python中的条件语句,分支语句以及逻辑运算符和比较运算符
- 详解Python中的元组与逻辑运算符
- [置顶] 关于python中的字符类型及逻辑运算符
- python学习4-流程控制-逻辑运算符
- python 逻辑运算符:not > and > or
- Python:逻辑运算符
- Python的逻辑运算符and小析
- Python 位运算符 逻辑运算符 成员运算符
- Python的关系运算符逻辑运算符和流程控制
- Python 学习->四类逻辑运算符
- python之路 - 逻辑运算符
- Python3学习三之逻辑运算符
- python 精简语法:连续逻辑运算符
- Python3--散列(集合,字典),逻辑运算符,运算符优先级