python中bool是什么意思_Python中的bool类型

Python 布尔类型 bool

python 中布尔值使用常量True 和 False来表示;注意大小写

比较运算符< > == 等返回的类型就是bool类型;布尔类型通常在 if 和 while 语句中应用

这边需要注意的是,python中,bool是int的子类(继承int),故 True==1  False==0 是会返回Ture的,有点坑,如要切实判断用 xxx is True

1 print(True==1) # 返回True

2 print(False==0) # 返回True

3 print(1 is True)

4 print(0 is False)

另外,还有几个坑。。。 如,Python2中True/False不是关键字,因此我们可以对其进行任意的赋值;同理,Python 中 if(True) 的效率远比不上 if(1)

True = "True is not keyword in Python2"        # Python2 版本中True False不是关键字,可被赋值,Python3中会报错

另,由于bool是int,可进行数字计算

print(True+True)

True or False 判定

以下会被判定为 False :

None

False

zero of any numeric type, for example, 0, 0.0, 0j.

any empty sequence, for example, '', (), [].

any empty mapping, for example, {}.

instances of user-defined classes, if the class defines a __bool__() or __len__() method, when that method returns the integer zero or bool value False.

除了以上的,其他的表达式均会被判定为 True,这个需要注意,与其他的语言有比较大的不同。

1 print(bool())

2 print(bool(False))

3 print(bool(0),bool(0.0),bool(0j))

4 print(bool(""),bool(()),bool([]),bool({}))

5 class alfalse():

6 def __bool__(self): # 定义了 __bool__() 方法,始终返回False

7 return False

8 f = alfalse()

9 print(bool(f))

10 class alzero():

11 def __len__(self): # 定义了 __len__() 方法,始终返回0

12 return 0

13 zero = alzero()

14 print(bool(zero))

15 class justaclass():

16 pass

17 c = justaclass()

18 print(bool(c)) # 一般class instance都返回为True

布尔运算符 and   or   not

Operation

Result

x

or

y

if

x

is false, then

y

, else

x

x

and

y

if

x

is false, then

x

, else

y

not

x

if

x

is false, then

True

, else

False

注意均为小写: and or not  ;  注意布尔运算的优先级低于表达式, not a == b 相当于 not (a == b), 若 a == not b 就会有语法错误

1 print((True and False),(False and False),(True and True))

2 print((True or False),(False or False),(True or True))

3 print((not True),(not False))

4 print( 1>1 and 1<1 ) # 表达式优于bool运算 相当于 print( (1>1) and (1<1) )

5 print( (1>1 and 1)<1) # 加括号了,值就不一样了

6 print(True and 1) # True and 数字,不建议这么使用,返回的是数字

7 print(True and 111)

8 print(False and 2) # False and 数字,返回False

9 print(not 1==1)

10 T = True

11 F = False

12 # print(T == not F) # 会报错

13 print(T == (not F)) # 需加括号

转载自:https://www.cnblogs.com/feeland/p/4360331.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pythonbool是布尔类型,用于表示真(True)或假(False)。\[1\]布尔类型在if和while语句经常被使用。在Pythonbool是int的子类,因此True可以被视为整数1,False可以被视为整数0,并且可以进行数字计算。\[1\]在判定,可以使用比较运算符(如<、>、==)来返回bool类型的结果。\[1\]注意,bool类型只有True和False两个实例,且区分大小写,首字母必须大写。\[3\]在Python,可以使用bool()函数来返回一个,即True或False。\[3\]在某些情况下,返回True的例子包括:非空的字符串、非零的数字、非空的列表等。而返回False的例子包括:空字符串、0、空列表等。\[3\] #### 引用[.reference_title] - *1* [【PythonPython数据结构之布尔类型bool)](https://blog.csdn.net/wzk4869/article/details/128751870)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python 布尔数据类型详解(bool)[学习 Python 必备基础知识][看此一篇就够了]](https://blog.csdn.net/manongajie/article/details/105581369)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python布尔类型bool)](https://blog.csdn.net/qq_53462109/article/details/127469158)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值