首页 > 编程语言 >Python - Values returned by and and or operators

Python - Values returned by and and or operators

时间:2024-07-29 21:06:08浏览次数:8  
标签:False Python operators value Values evaluated operand so first

Unlike some other languages, in Python, the logical operators and and or do not return Boolean values True or False; they actually return the last evaluated operand. We generally use these operators in if and while conditions, so we do not get to know what they return exactly because, in those cases, only their truth value is used. Let us see what they actually return.

>>> 0 and 4

0

>>> 4 and 8

8

>>> 0 or 4

4

>>> 4 or 8

4

For and operator, the second operand is not evaluated if the first one is False.

In the expression 0 and 4, the interpreter evaluates the first operand; it is False, so there is no need to evaluate the second operand. 0 is the last evaluated operand and so it is returned.

In the expression 4 and 8, the first operand is True, so the second operand has to be evaluated, and so here, 8 is the last evaluated operand, and it is returned.

For or operator, the second operand is not evaluated if the first one is True.

In the expression 0 or 4, the first operand is False, so the second operand has to be evaluated. 4 is the last evaluated operand, so it is returned.

In the expression 4 or 8, the first operand is True; there is no need to evaluate the second one. Thus, 4 is the last evaluated operand, so it is returned.

The expression operand1 and operand2 first evaluates operand1; if it is False, its value is returned; otherwise, operand2 is evaluated and its value is returned.

The expression operand1 or operand2 first evaluates operand1; if it is True, its value is returned; otherwise, operand2 is evaluated, and its value is returned.

These operators actually return operands, but most of the time, they are used in a Boolean context, so only their truth value is used. The fact that they return the last evaluated argument can be used by programmers in certain situations.

Suppose we have a string s, and if it is empty, it has to be replaced by a default value, 'NA'. We can write this:

s = s or 'NA'

If the string s is empty, the first operand s will be False, so the second operand will be evaluated, and it becomes the value of the expression. If the string s is not empty, the second operand will not be evaluated, and the value of the expression s or 'NA' will be s only. Here is another example:

average = count!=0 and total/count

Here, we are finding the average and guarding our division by using the and operator. If count is not equal to zero, the first operand will be True, so the second operand will be evaluated, and its value will be returned and assigned to average.

If count is equal to 0, the first operand will be False, so the second operand will not be evaluated, thus avoiding divide by zero error. The value of the first operand will be assigned to average. So, average will be assigned False. In Python, False is numeric value 0, and True is numeric value 1. So, when we use this average in mathematical context, value 0 is used.

The operator not always returns Boolean value True or False; True if its argument is falsy, False if its argument is Truthy.

 

标签:False,Python,operators,value,Values,evaluated,operand,so,first
From: https://www.cnblogs.com/zhangzhihui/p/18331080

相关文章

  • ICEEMDAN算法 python代码实现
    1.安装matlab.enginepython库里没有ICEEMDAN的方法,需要通过python调用matlab的库中的ICEEMDAN。首先下载python和matlab(这里就不过多阐述了),python和matlab的版本要对应,下面是python和matlab对应的版本(仅供参考)(要记住matlab安装的位置,下面要用)从anacondapropmt进入自己创建......
  • 基于Python进行小波分析
    在气象学和环境科学的研究中,理解和预测气象数据的周期性变化至关重要。小波分析作为一种高效的数学工具,近年来在气象数据的周期性分析中得到了广泛应用。本文将详细介绍如何通过Python进行小波分析,以探究气象数据中的周期性变化。1数据来源及下载方式西北农林科技大学的彭守璋......
  • Python - Using a list with functions from the random module
    Toselectarandomitemfromthelistorshufflethelist,youcanusethechoiceandshufflefunctionsfromtherandommoduleofthestandardlibrary.Therandom.choice()functionreturnsarandomlyselectedelementfromthelist.>>>importran......
  • Python 教程(六):函数式编程
    目录专栏列表前言函数定义参数返回值示例函数类型普通函数空函数匿名函数(Lambda函数)嵌套函数函数装饰器高阶函数函数参数位置参数默认参数可变位置参数可变关键字参数函数属性和方法`__name__``__doc__``func.__dict__``func.__defaults__``func.__annotations__`函......
  • Python操作MySQL数据库的5种方式
    不管你是做数据分析,还是网络爬虫,Web开发、亦或是机器学习,你都离不开要和数据库打交道,而MySQL又是最流行的一种数据库,这篇文章介绍Python操作MySQL的5种方式,你可以在实际开发过程中根据实际情况合理选择。1、MySQLdbMySQLdb又叫MySQL-python,是Python连接MySQL最流行......
  • Python自定义排序
    Python封装了成熟的排序函数,我们只需要调用内部的sort函数,就可以完成排序。但是实际场景当中,排序的应用往往比较复杂,比如对象类型,当中有多个字段,我们希望按照指定字段排序,或者是希望按照多关键字排序,这个时候就不能简单的函数调用来解决了。1.字典排序我们先来看下最常见的字典......
  • Python中清空list的几种方法
    本文介绍清空list的四种方法,以及list=[]和list.clear()在使用中的区别(坑)。1、使用clear()方法lists=[1,2,1,1,5]lists.clear()print(lists)>>>[]2、重新初始化列表:初始化该范围内的列表,初始化列表没有值,即大小为0的列表lists=[1,2,1,1,5]lists=[]print......
  • 小一保姆级 python三大核心多态、抽象类、动态添加内容详解
    一.多态多态是面向对象编程中的一个核心概念,它允许一个接口被多个数据类型实现。这意味着,即使多个类具有不同的内部实现,它们也可以共享一个公共接口。多态的实现通常依赖于继承和方法重写。继承:子类继承父类的属性和方法。方法重写:子类重写父类中的方法,以提供特定的实现。......
  • python找出字典中value最大值的几种方法
    假设定义一字典,m={"a":3,"e":6,"b":2,"g":7,"f":7,"c":1,"d":5},在不知道key的情况下如何找出字典中value最大的所有key-value对?下面讨论几种方法。1)通过m.values()和max()函数第一步,通过max()函数找到字典中的value最大值。max(m.values())结果为7第二步,再通......
  • 计算机毕业设计选题推荐-音乐播放系统-Java/Python项目实战
    ✨作者主页:IT毕设梦工厂✨个人简介:曾从事计算机专业培训教学,擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。☑文末获取源码☑精彩专栏推荐⬇⬇⬇Java项目Python项目安卓项目微信小程序项目......