当前位置: 首页 > 图灵资讯 > 行业资讯> 小白必看的python中的Bool运算和真假值

小白必看的python中的Bool运算和真假值

发布时间:2025-09-22 10:56:23

任何对象都可以在python中判断其真假价值:True,False

在if或while条件判断中,以下情况值为false:

1.None

2.Flase

3.数值为0的情况,如:0,0.0,0j

4.所有空序列,如:'',(),[]

5.所有空mapping,如:{}

6.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.

All other values are considered true — so objects of many types are always true.

返回Boolean结果0或Flase在运算操作和内建函数中表示false(更多学习内容,请点击Python学习网)

或者True表示true

Boolean在python中的运算如下:

print('xory->ifxisfalse,theny,elsex')
x,y=2,0
print('{}or{}{}'.format(x,y,xory))
x1,y1=0,10
print('{}or{}{}'.format(x1,y1,x1ory1)
x2,y2=0,0
print('{}or{}{}'.format(x2,y2,x2ory2)

print('#'*50)
print('xandy->ifxisfalse,thenx,elsey')
print('{}and{}{}'.format(x,y,xandy))
x1,y1=0,10
print('{}and{}{}'.format(x1,y1,x1andy1)
x2,y2=0,0
print('{}and{}{}'.format(x2,y2,x2andy2)

print('#'*50)
print('notx->ifxisfalse,thenTrue,elseFalse')
x=2
print('not{}{}'.format(x,notx))
x=0
print('not{}{}'.format(x,notx))

运行结果:

>>>
xory->ifxisfalse,theny,elsex
or0=2
or10=10
or0=0
##################################################
xandy->ifxisfalse,thenx,elsey
and0=0
and10=0
and0=0
##################################################
notx->ifxisfalse,thenTrue,elseFalse
not2=False
not0=True
>>>

相关文章

Python中reduce函数和lambda表达式的学习

Python中reduce函数和lambda表达式的学习

2025-09-25
Python小白必学的面向对象

Python小白必学的面向对象

2025-09-25
一个例子解释python装饰器

一个例子解释python装饰器

2025-09-25
深入理解Python的set和dict

深入理解Python的set和dict

2025-09-25
Python中正则表达式的巧妙使用

Python中正则表达式的巧妙使用

2025-09-25
5分钟搞定Python中函数的参数

5分钟搞定Python中函数的参数

2025-09-25