python中Bool运算和真假值的示例

这篇文章将为大家详细讲解有关python中Bool运算和真假值的示例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比义乌网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式义乌网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖义乌地区。费用合理售后完善,十载实体公司更值得信赖。

在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(更多学习内容,请点击创新互联)

1或True表示true

python中的Boolean运算如下:

print('x or y -> if x is false,then y, else x ')
x, y= 2,0
print('{} or {} = {}'.format(x, y, xor y))
x1, y1= 0,10
print('{} or {} = {}'.format(x1, y1, x1or y1))
x2, y2= 0,0
print('{} or {} = {}'.format(x2, y2, x2or y2))
  
print('#' * 50)
print('x and y -> if x is false,then x, else y ')
print('{} and {} = {}'.format(x, y, xand y))
x1, y1= 0,10
print('{} and {} = {}'.format(x1, y1, x1and y1))
x2, y2= 0,0
print('{} and {} = {}'.format(x2, y2, x2and y2))
  
print('#' * 50)
print('not x -> if x is false,then True,else False ')
x= 2
print('not {} = {}'.format(x,not x))
x= 0
print('not {} = {}'.format(x,not x))

运行结果:

>>>
x or y -> if x is false,then y, else x
or 0 = 2
or 10 = 10
or 0 = 0
##################################################
x and y -> if x is false,then x, else y
and 0 = 0
and 10 = 0
and 0 = 0
##################################################
not x -> if x is false,then True,else False
not 2 = False
not 0 = True
>>>

关于python中Bool运算和真假值的示例就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

文章题目:python中Bool运算和真假值的示例
URL地址:https://www.cdcxhl.com/article12/ijjcgc.html

成都网站建设公司_创新互联,为您提供网站内链商城网站网站维护建站公司企业网站制作品牌网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化