码迷,mamicode.com
首页 > 编程语言 > 详细

Python多个and和or的逻辑判断

时间:2020-03-24 14:29:40      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:print   逻辑   bsp   返回   str   计算   没有   lib   style   

如果一个语句中遇到多个andor的情况下该如何判断执行的顺序呢?

基本逻辑:

 

有括号的先计算括号内的;

 

执行顺序:

 

从前到后开始执行;

 

执行结果:

 

如果第一个结果为True后面是or,那么最终结果是True

 

a = True

b = False

c = False

if a or b and c:

print(123)

123

如果True后面是and,后面继续判断;

 

a = True

b = False

c = False

if a and b or c: # a and b 返回False

print(123) # False or c 返回False

# 没有输出

 

a = True

b = False

c = True

if a and b or c: # a and b 返回False

    print(123) # False or c 返回True

123

如果False后面是or,继续向后判断;

 

a = False

b = True

c = False

if a or b and c: # a or b 返回True

print(123) # True and c 返回False

# 没有输出

 

a = False

b = True

c = True

if a or b and c: # a or b 返回True

    print(123) # True and c 返回True

123

如果False后面是and,继续向后判断;

 

a = False

b = True

c = True

if a and b or c: # a and b 返回False

print(123) # False or c 返回 True

123

 

Python多个and和or的逻辑判断

标签:print   逻辑   bsp   返回   str   计算   没有   lib   style   

原文地址:https://www.cnblogs.com/hjhlg/p/12558798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!