布尔类型&运算逻辑符&短路&运算符优先级 python笔记day05

布尔类型

  • bool()函数的返回值只有True和False
  • bool()函数里面的参数是字符串时,只有空字符串的结果是返回False,其余都是True
>>> bool(250)
True
>>> bool("假")
True
>>> bool("False")
True
>>> bool("")
False
  • bool()函数里面的参数是数值时,只有值等于0的结果才是False,其余都是True
>>> bool(52)
True
>>> bool(0.1)
True
>>> bool(0.0)
False
>>> bool(0)
False
>>> bool(0j)
False
  • bool()函数返回False的情况:
    • 定义为False的对象:None和False
    • 值为0的数字类型:0,0.0,0j,Decimal(0),Fraction(0,1)(表示分数,分母为0,分子为1的分数)
    • 空的序列和集合:‘’,(),[],{},set(),range(0)
>>> bool(False)
False
>>> bool(None)
False
>>> bool(0)
False
>>> bool(0.0)
False
>>> bool(0j)
False
>>> import decimal
>>> bool(decimal.Decimal(0))
False
>>> from fractions import Fraction
>>> bool(Fraction(0,1))
False
>>> bool("")
False
>>> bool(())
False
>>> bool({})
False
>>> bool([])
False
>>> bool(set())
False
>>> bool(range(0))
False

在选择语句中使用

>>> if 520 > 250:
...    print("520比250大")
... else:
...    print("520不比250大")
...
520250>>> if bool(250):
...    print("250 is True")
... else:
...    print("250 is False")
...
250 is True

bool类型是特殊的整数类型

>>> 1 == True
True
>>> 0 == False
True
>>> True + False
1
>>> True - False
1
>>> True * False
0
>>> True / False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero#报错原因,除数不能为0

逻辑运算符

  • 逻辑运算也叫布尔运算,它的运算对象是布尔类型的对象
  • 逻辑运算可以将多个比较的结果合并在一起进行判断
运算符含义
and左边和右边同时为True,结果为True
or左边或者右边其中一个为True,结果为True
not如果操作数为True,结果为False;如果操作数为False,结果为True
>>> 3 < 4 and 4 < 5 #True and True
True
>>> 3 > 4 and 4 < 5 # False and True
False
>>> 3 > 4 or 4 < 5 #False or True
True
>>> 3 > 4 or 4 > 5 #False or False
False
>>> 3 < 4 or 4 < 5 #True or True
True
>>> not True
False
>>> not False
True
>>> not 283
False
>>> not 0
True

由于python的所有对象都支持真值测试,所以操作数事实上支持任何对象,它将对对象的真值测试结果进行逻辑运算,如果你给到操作数的是两个数值,那么它的运算结果也是数值;如果你给到的操作数是两个字符串,那么它的结果也是字符串

>>> 3 and 4
4
>>> 5 or 4
5
>>> 250
250
>>> "python" and "love"
'love'

短路逻辑

核心思想:从左往右,只有当第一个操作数的值无法确定逻辑运算的结果时,才对第二个操作数进行求值。

>>> 3 and 4
4
>>> 5 or 4
5

1.“3 and 4”语句,由于 and运算符的操作结果是只有左右两个操作数的结果同时为True结果才为True,那么左边3是True,还要继续判断右边的布尔类型才能得出最后的结果。所以右边结果影响了最后的结果,python就把右边的值直接给扔出来当作返回值

2.“5 or 4” 语句,由于or运算符左边或者右边其中一个为True,结果为True,所以判断5为True,那么结果一定为True,python就直接把5作为返回值

>>> 0 and 5
0
>>> 0 or 4
4

在这里插入图片描述

运算符优先级

优先级运算符描述
1(expressions…),[expressions…], {key: value…}, {expressions…}圆括号的表达式
2x[index], x[index:index], x(arguments…), x.attribute读取,切片,调用,属性引用
3await xawait 表达式
4**乘方(指数)
5+x, -x, ~x正,负,按位非 NOT
6*, @, /, //, %乘,矩阵乘,除,整除,取余
7+, -加和减
8<<, >>移位
9&按位与 AND
10^按位异或 XOR
11|按位或 OR
12in,not in, is,is not, <, <=, >, >=, !=, ==比较运算,包括成员检测和标识号检测
13not x逻辑非 NOT
14and逻辑与 AND
15or逻辑或 OR
16if – else条件表达式
17lambdalambda 表达式
18:=赋值表达式

注意: 优先级 not>and>or

>>> 0 or 1 and not 2
#先执行not 2 ,变成0 or 1 and false, 然后执行1 and false,变成0 or false,最后结果false
False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值