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

发布时间:2020-11-25 13:43:20 作者:小新
来源:亿速云 阅读:175

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

在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运算和真假值的示例就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 什么是javaScript中的真假值以及相等操作符
  2. python中的bool的意思是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python booll bo

上一篇:python中*重复符的使用示例

下一篇:Python大小写转换,首字母大写,去除特殊字符的示例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》