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 - Config Tips

    Guideline: Jmeter.properties file should avoid to be modified , modified user.properties instead > ...

  2. win10+py38环境分分钟装好geopandas

    python版本是anaconda自带3.8,尝试了下面这个网上最推荐的安装方法 conda install --channel conda-forge geopandas 但多次以失败告终,看了几个 ...

  3. 有null值的子查询,子查询加排序,速度大幅提升

    意外发现,如果子查询结果有大量null值,加上一个排序,最后查询速度飞起

  4. Vulnhub 靶场 DIGITALWORLD.LOCAL: SNAKEOIL

    Vulnhub 靶场 DIGITALWORLD.LOCAL: SNAKEOIL 前期准备 靶机地址:https://www.vulnhub.com/entry/digitalworldlocal-sn ...

  5. 清空buff/cache

    https://blog.csdn.net/qq_34246965/article/details/109258656 1)清理pagecache(页面缓存)echo 1 > /proc/sys ...

  6. java资源精华

    1. java基础教程虚拟机性能分析和故障解决工具_哔哩哔哩_bilibili 2.https://www.bilibili.com/video/BV1Eb4y1v7mB/?p=2&vd_so ...

  7. 性能测试记录: ZZ 只改5行代码获得10倍吞吐量提升

    首先得找台足够性能的机器来测试,性能不足时代码运行会出现各种奇怪的现象,导致浪费时间 文章: https://www.jianshu.com/p/4cd8596352ad   只改了5行代码吞吐量提升 ...

  8. springboot配置文件中的基本配置

    #应用启动端口设置server.port=9088#=================================微信相关====================================# ...

  9. tsc条码打印机如何导入表格批量打印

    很多时候,我们在TSC条码打印机的权昌条码打印软件里做标签时,涉及数据特别大,都保存在EXCLE表格里,那要怎么做,才可以使软件批量打印EXCEL数据呢?下面,小编就教教大家一种简单的批量打印标签的方 ...

  10. Day 11 11.1 Xpath解析

    xpath解析 xpath在Python的爬虫学习中,起着举足轻重的地位,对比正则表达式 re两者可以完成同样的工作,实现的功能也差不多,但xpath明显比re具有优势,在网页分析上使re退居二线. ...