python中3not5_python3 逻辑运算符

本文介绍了Python3中的逻辑运算符,包括'and'、'or'和'not'的用法,并通过实例解析了它们的计算过程和优先级。例如,10>1 and 3>1返回True,而10<1 or 3>1也返回True。同时,详细阐述了逻辑运算符的优先级:() > not > and > or。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、and

"与" ,如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。

10<1 and 3>1 #10<1为False,所以整体为False

---------------------------False

10>1 and 3>1 #10>1为True,所以看3>1的布尔值,3>1为True,则整体值为True.

----------------------------------------------------------------------------------------True

2、or

"或",如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。

10>1 or 3>1. #10>1位True,则直接返回10>1的布尔值。

----------------------------------------------------------------True

10<1 or 3>1. #因为10<1位False, 则返回3>1的布尔值。

-----------------------------------------------------------------True

3、not

"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。

notTrue--------------------------False

4、逻辑运算符的优先级

从左到右:() > not > and > or

计算:1>3 or 3>2 and 1=0 and not(7>2 or 3>5)

解答:

从左到右,先计算()里,7>2为True,则括号里为True,在看括号外面的not,则这一部分#为false.

再先计算左边的and,3>2为True,则看1=0的布尔值,1=0为False,则3>2 and 1=0为#False.

再计算 3>2 and 1=0 and not(7>2 or 3>5),已知3>2 and 1=0 为False, not(7>2 or 3>5)为False,则3>2 and 1=0 and not(7>2 or 3>5)为False。

最后看1>3为False,3>2 and 1=0 and not(7>2 or 3>5)为False,则整体为False.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值