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. Jmeter固定吞吐量控制器Constant Throughput Timer

    控制请求的TPS,可以使用JMETER的固定吞吐量控制器Constant Throughput Timer Target throughput(in samples per minute):目标吞吐量 ...

  2. elasticSearch(四)--结构化查询

    结构化查询 1.请求体查询 GET(POST) /_search POST /_search { "from": 30, "size": 10 } 2.DSL ...

  3. 第6课第4节_Binder系统_驱动情景分析_服务注册过程_分析

    Service_manager.c 8074 2017/6/30 首先从应用程序分析(Android) binder_loop(bs, svcmgr_handler) { readbuf[0] = B ...

  4. 对于小白如何才能学好Java

    我们学习任何一种编程语言,不是去无目的的网上找太多的教程,而是首先从宏观了解它的基本思想方法定义概念,从宏观感受它的逻辑与抽象,然后是实践,不断的实践.今天我们看看网上给我们小白的一些学习建议. 一. ...

  5. TCP 协议之三次握手(一)

    (声明:本文是阅读此文的总结笔记) 面试中对于该知识点的考察频繁,一般作为面试的暖场问题,你能够答出多少,答得多深刻,条理多清晰,给面试官的印象真的很重要. 三次握手的过程 TCP是面向连接的,全双工 ...

  6. 【JVM】学习JVM垃圾回收理论

    参考链接:https://www.cnblogs.com/aspirant/p/8662690.html 一,垃圾回收算法 JVM内存结构:程序计数器.虚拟机栈.本地方法栈.堆区.方法区 1,引用计数 ...

  7. macOS 常用键盘快捷键大全

    对于初次接触 macOS 的朋友来说,除了要寻找不同的 APP 软件之外,还有一件事情也直接影响着使用电脑的效率,那就是 - 键盘快捷键! 与 Windows 的差异 我们先来认识一下苹果 Mac 键 ...

  8. Hashtable多线程遍历问题

    If a thread-safe implementation is not needed, it is recommended to use HashMap in place of code Has ...

  9. js-模态框的拖动

    效果如下: 代码如下: <div class="clickBtn">点击,弹出登录框</div> <div class="login&quo ...

  10. jquery获取父级容器高度

    //获取浏览器显示区域的高度console.log( $(window).height()); //获取浏览器显示区域的宽度console.log($(window).width()); //获取页面 ...