首页 > 编程语言 >Python:Short Circuiting -- “OR”

Python:Short Circuiting -- “OR”

时间:2024-02-21 20:35:03浏览次数:25  
标签:Short False Python true value -- True

Short Circuiting

Operator Checks if: Evaluates from left to right up to: Example
AND All values are true The first false value False and 1 / 0 evaluates to False
OR At least one value is true The first true value True or 1 / 0 evaluates to True

Short-circuiting happens when the operator reaches an operand that allows them to make a conclusion about the expression. For example, and will short-circuit as soon as it reaches the first false value because it then knows that not all the values are true.

If and and or do not short-circuit, they just return the last value; another way to remember this is that and and or always return the last thing they evaluate, whether they short circuit or not. Keep in mind that and and or don't always return booleans when using values other than True and False.

举例

---------------------------------------------------------------------
The Truth Will Prevail > Suite 2 > Case 2
(cases remaining: 2)

What would Python display? If you get stuck, try it out in the Python
interpreter!

>>> print(3) or ""

分析

因为操作符or的短路特性,会让这个操作符寻找第一个值为True的值。如果左边的值不为True,那么就会执行右边的值。

这里print()返回为None,同等于逻辑False。这样or便会执行下一个表达式""。这样便会输出'',即空字符串。

标签:Short,False,Python,true,value,--,True
From: https://www.cnblogs.com/shangshankandashu/p/18026140

相关文章

  • 数组常用方法
    foreach和之前for循环作用基本一样,只不过比for更灵活方便一些参数:回调函数,该回调函数有三个参数遍历项索引该数组indexof用于查找在数组中的位置,返回值为索引,如果没有找到返回-1参数:第一个参数为要查找的数据第二个参数为从哪个位置开始constarr=[1,2,3,4,5]......
  • 第十八节:动态规划面试题(爬楼梯、买卖股票时机、最大子数组和)
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • 阿***不会被现实磨平棱角你不是这世界的人没必要在乎真相命运多舛痴迷淡然挥别了青春数不尽的车站甘于平凡却不甘平凡的腐烂你是阿***你是自由的鸟——《阿***》感觉的确是有生以来经历的最大的一场雪了。晚上下课,同学说要去雪里走一走。刚走到一块完整的区域,我心......
  • js 基础知识
     01-数据类型值类型(基本类型):字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol。引用数据类型(对象类型):对象(Object)、数组(Array)、函数(Function),还有两个特殊的对象:正则(RegExp)和日期(Date)。02-检测数据类型2.1-typeof<!DOCTYPEhtml><ht......
  • ctfshow-ssrf
    web351<?phperror_reporting(0);highlight_file(__FILE__);$url=$_POST['url'];$ch=curl_init($url);curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);$result=curl_exec($ch);curl_close($ch);echo($result);?&g......
  • windows安装bun.js
    1.下载bun可执行文件,地址如下https://github.com/oven-sh/bun/releases/download/canary/bun-windows-x64.zip 2.解压到D盘修改文件夹名为bun并且创建快捷方式 3.增加环境变量 4.验证  bun--help&bun-v......
  • 可视化
    可视化最基本的形式就是简单地把数据映射成彩色图形。它的工作原理就是大脑倾向于寻找模式,你可以在图形和它所代表的数字间来回切换。1985年,AT&T贝尔实验室的统计学家威廉·克利夫兰(WilliamCleveland)和罗伯特·麦吉尔(RobertMcGill)发表了关于图形感知和方法的论文[18]。研究焦点......
  • 闭包及原理
    概念一个函数和对其周围状态(lexicalenvironment,词法环境)的引用捆绑在一起(或者说函数被引用包围),这样的组合就是闭包(closure)。大白话也就是说,闭包让你可以在一个内层函数中访问到其外层函数的作用域点击查看代码functionfn(){vara=10returnfunctiongetData(){......
  • Go - argument evaluation with defer
        ......
  • 程序是怎么跑起来的第六章
    文件就是字节数据的集合,如果文件中储存的数据是文字,该文件就是文本文件。如果是图形,该文件就是就是图像文件。在半角字母中,一个字符是作为一个字节的数据保存在文件中AAAAAABBCDDEEEEEF可以用A6B2C1D2E5F1表示。A6B2C1D2E5F1是12个字符也就是12字节,因此结果就将原文件压缩了12......