Python正课11 —— 逻辑运算

本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12425769.html

一:逻辑运算符

逻辑运算符用于连接多个条件,进行关联判断,会返回布尔值True或False

1.not

逻辑 非,也就是取反

偷懒原则:not 就是:真变假,假变真

print(not 1) #1在逻辑运算中代表True,not 1 就是 not True
False

print(not 0) #1在逻辑运算中代表False,not 0 就是 not False
True

2.and

逻辑 与

偷懒原则:and 就是:全真为真,一假即假

print(1 and 4>1 and True)
True

print(3>4 and 0 and False and 1)
False

3.or

逻辑 或

偷懒原则:or 就是:一真即真,全假为假

print(1 or 4>1 or True)
1 #1在逻辑运算中代表True

print(3>4 or 0 or False)
False

二:优先级

not > and > or

PS:如果单独就只是一串and连接,或者单独就只是一串or连接,按照从左到右的顺序运算

PS:如果是混用,则需要考虑优先级了

()拥有最高优先级,“()”内的内容直接提升到第一优先级,先运算

posted @ 2020-03-06 12:00  轻描丨淡写  阅读(408)  评论(0编辑  收藏  举报