and 判断非Boolean类型数据会自动转换类型

"A" and "B" → "B"

因为表达式 A 和 B都为True所以返回 "B"

"A" is True → False

因为这里判断的"A": str类型,而True为Boolean类型所以不相等

bool("A") is True → True

这里将"A"装换为Boolean类型后就可以判断成功了

所以得出结论 and 关键字在做数据判断时会将其装换为Boolean类型后,再进行判断

以下是官方定义的False对象

Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

None

False


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


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


any empty mapping, for example, {}.


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

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

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

Python Boolean类型 判断的更多相关文章

  1. Python字符串类型判断错误

    Python里面常用的字符串类型有str和unicode,如果要判断一个对象的类型,最好用basestring,否则可能会判断错误: str1 = "hello" str2 = u ...

  2. JS判断时特殊值与boolean类型的转换

    扒开JQuery以及其他一些JS框架源码,常常能看到下面这样的判断,写惯了C#高级语言语法的我,一直以来没能系统的理解透这段代码. var test; //do something... if(tes ...

  3. 【Java】if判断boolean类型

    public class Test { public static void main(String[] args) { boolean i=false; boolean j=true; if(i=j ...

  4. python的数据结构分类,以及数字的处理函数,类型判断

    python的数据结构分类: 数值型 int:python3中都是长整形,没有大小限制,受限内存区域的大小 float:只有双精度型 complex:实数和虚数部分都是浮点型,1+1.2J bool: ...

  5. python学习_循环结构 and 类型判断

    # 循环结构 ### 循环结构(while) - 格式 ```python while 表达式: 语句块 ``` > 执行流程:当程序执行到while语句时,首先判断表达式的真假.若表达式的值为 ...

  6. Python列表操作与深浅拷贝(5)——数字处理函数、类型判断、列表链表队列栈

    python内建数据结构 分类 数值型: int float complex bool 序列对象: 字符串str 列表list 元组tuple 键值对: 集合set 字典dict 数值型 (list ...

  7. JS中字符串的true转化为boolean类型的true

    var a="True"; a = eval(a.toLowerCase()); alert(typeof a); //boolean alert(a);//true 正解,eva ...

  8. php变量的判空和类型判断

    (1)var_dump(); 判断一个变量是否已经声明并且赋值,并且打印类型和值 <?php $a; var_dump($a);//输出null <?php var_dump($a);// ...

  9. [改善Java代码]在equals中使用getClass进行类型判断

    建议47: 在equals中使用getClass进行类型判断 本节我们继续讨论覆写equals的问题.这次我们编写一个员工Employee类继承Person类,这很正常,员工也是人嘛,而且在JEE中J ...

  10. JavaScript 类型判断 —— typeof 以及 instanceof 中的陷阱

    JavaScript中基本类型包含Undefined.Null.Boolean.Number.String以及Object引用类型.基本类型可以通过typeof来进行检测,对象类型可以通过instan ...

随机推荐

  1. testtesttest

    test test       2022-09-24 #include<iostream> using namespace std; int main(){ cout<<&qu ...

  2. 解决在高分屏电脑上的vmware,linux系统的显示比例不正确的问题

    除了在虚拟机系统内改变显示比例为200%的方法,还有另一种方法: 编辑虚拟机设置--硬件--显示器--指定监视器设置,选择任意监视器的最大分辨率为1920x1080(或者比例保持不变的其他分辨率,例如 ...

  3. Xilinx-英文缩写

    BEL: Basic Element of Logic, BEL是最底层的基本元素,也可以叫atomic unit(原子单位),BEL是FPGA中最小.不可分割的组件. ACP:Accelerator ...

  4. 【转】BIO,NIO和AIO

    本文转自:https://blog.csdn.net/qxy_1218/article/details/123941039 BIO,NIO和AIO是Java网络编程的三种模型 BIO:同步并阻塞,服务 ...

  5. 第14章 Windows管理规范

    第14章 Windows管理规范 我们一直期望但是又害怕写这一章.Windows管理规范(Windows Management Instrumentation,WMI)可能是微软提供给管理员使用最优秀 ...

  6. springboot+mybais配置多数据源(分包实现)

    一.分包方式实现: 1.在application.properties中配置两个数据库: #druid连接池 #dataSoureOne(这里是我本地的数据源) spring.datasource.o ...

  7. 微信字体大小调整导致的H5页面错乱问题处理

    当用户调整微信字体大小时会导致H5页面错乱,解决方案如下: ios:在css中加入-webkit-text-size-adjust: 100% !important;   body {   -webk ...

  8. linux下nginx的(启动、重启、关闭)

    1. 首先利用配置文件启动nginx. 命令: nginx -c /usr/local/nginx/conf/nginx.conf 重启服务: service nginx restart 2. 快速停 ...

  9. MFC编辑框字符显示时无法换行的问题解决

    字符串结尾加上"\r\n": 编辑框属性设置:Auto HScroll为False,Multiline为True,Want Return为True.

  10. Kubernetes--管理Pod对象的容器(2)

    自定义运行的容器化应用 由Docker镜像启动容器时运行的应用程序在相应的Dockerfile中由ENTRYPOINT指令进行定义,传递给程序的参数则通过CMD指令指定,ENTRYPOINT指令不存在 ...