python 逻辑运算符

Relation and Logic are the fundamental bricks of a program that defines its functionality. With these fundamentals, you decide what should be the flow of execution and what conditions should be kept to make sure the flow stays that way.

关系和逻辑是定义其功能的程序的基本组成部分。 有了这些基本知识,您就可以决定执行流程应该是什么,应该保持什么条件以确保流程保持这种状态。

In every programming language including python, to manage the flow of any program, conditions are required, and to define those conditions, relational and logical operators are required.

在包括python在内的每种编程语言中,要管理任何程序的流程,都需要条件,并且要定义这些条件,则需要关系和逻辑运算符。

Remember those days when your mathematics teacher in school used to ask you if 3 is greater than 2, say yes, otherwise no, that is pretty much what we do in programming world too.

记得那些日子,当您学校的数学老师曾经问过您3是否大于2时,说是,否则,这在我们编程世界中也差不多。

You provide the compiler with some condition based on an expression, compiler computes the expression and executes the condition based on the output of the expression. In the case of relational and logical expressions, the answer will always be either True or False.

您为编译器提供了基于表达式的某些条件,编译器将计算表达式并根据表达式的输出执行条件。 对于关系和逻辑表达式,答案将始终为TrueFalse

Operators are the conventional symbols, that bring one, two or more operands together to form an expression. Operators and operands are two key deciding factors of the output.

运算符是常规符号,它们将一个,两个或多个操作数组合在一起以形成表达式。 运算符和操作数是输出的两个关键决定因素。

Now let's see some operators that are available in python language.

现在,让我们看一些可用python语言提供的运算符。

Python关系运算符 (Python Relational Operator)

Relational operators are used to establish some sort of relationship between the two operands. Some of the relevant examples could be less than, greater than or equal to operators. Python language is capable of understanding these types of operators and accordingly return the output, which can be either True or False.

关系运算符用于在两个操作数之间建立某种关系。 一些相关示例可能小于大于等于运算符。 Python语言能够理解这些类型的运算符,并相应地返回输出,可以是TrueFalse

Let's checkout a few relational expressions. Open your IDLE and try this:

让我们检查一些关系表达式。 打开您的IDLE,然后尝试以下操作:

>>> 5 < 9

True

真正

Since 5 is less than 9, thus the output returned is True.

由于5小于9 ,因此返回的输出为True

The list of operators available includes:

可用的运算符列表包括:

  1. Less than → used with <

    小于 →与<

  2. Greater than → used with >

    大于 →与>

  3. Equal to → used with ==

    等于 →与==

  4. Not equal to → used with !=

    不等于 →与!=

  5. Less than or equal to → used with <=

    小于或等于 →与<=

  6. Greater than or equal to → used with >=

    大于或等于 →与>=

You can try each of the operators to practice with some numbers (or even strings).

您可以尝试使用每个运算符来练习一些数字(甚至是字符串)。

>>> "abc" > "aaa"
>>> "abc" == "bcd"

True False

真假

Python逻辑运算符 (Python Logical Operators)

Logical operators, as the name suggests are used in logical expressions where the operands are either True or False. The operands in a logical expression, can be expressions which returns True or False upon evaluation. There are three basic types of logical operators:

顾名思义,逻辑运算符用于操作数TrueFalse逻辑表达式中。 逻辑表达式中的操作数可以是在求值时返回TrueFalse表达式。 逻辑运算符有三种基本类型:

  1. Logical AND: For AND operation the result is True if and only if both operands are True. The keyword used for this operator is and.

    逻辑与 :对于AND运算结果为True当且仅当两个操作数均为True 。 用于此运算符的关键字是and

  2. Logical OR: For OR operation the result is True if either of the operands is True. The keyword used for this operator is or.

    逻辑OR :对于OR运算,如果两个操作数中的任何一个为True则结果为True 。 用于此运算符的关键字是or

  3. Logical NOT: The result is True if the operand is False. The keyword used for this operator is not.

    逻辑非 :如果操作数为False则结果为True 。 用于此运算符的关键字not

Let's see a few examples:

让我们看几个例子:

>>> True and False

False

>>> not True

False

Now, we also know that the relational expressions return a Boolean value as their output, therfore know we can combine relational and logical expressions to create something more meaningful. For example,

现在,我们还知道关系表达式返回布尔值作为其输出,因此知道我们可以结合使用关系表达式和逻辑表达式来创建更有意义的东西。 例如,

>>> (2 < 3) and (2 < 5)

True

真正

>>> (2 < 3) and (2 < 1)

False

Taking a bit more realistic programming example, consider you have a variable x as input and you want to check if the user entered value is between some range, say 0 to 100, then:

以更现实的编程示例为例,假设您有一个变量x作为输入,并且您要检查用户输入的值是否在某个范围内(例如0到100),然后:

>>> x = int(input())

25

25

>>> (x > 0) or (x < 100)

True

真正

翻译自: https://www.studytonight.com/python/relational-and-logical-operators

python 逻辑运算符

python 逻辑运算符_Python关系和逻辑运算符相关推荐

  1. python关系运算符可以连续使用_python学习笔记-字符串拼接关系运算符逻辑运算符...

    #字符串拼接&关系运算符&逻辑运算符 print()函数 #print()函数输出时候,可以指定多个输出值之间的分隔符,参数sep='' 设置 print('a','b','c',se ...

  2. python逻辑运算符不懂_Python运算符之逻辑运算符

    在Python中,有三种逻辑运算符:逻辑与,逻辑或和逻辑非.它们用于逻辑运算并返回一个布尔值. 1.基本用法 (1)and : 逻辑与 and运算需要两个操作数,如果参与运算的两个操作数都为True, ...

  3. linux运算_CentOS「linux」学习笔记22:算术运算符、逻辑运算符、关系运算符

    ​linux基础操作:主要介绍啦算术运算符.逻辑运算符.关系运算符 1.算术运算符[主要用来计算数值] 注意使用expr运算时运算符和数值之间需要有空格,其他方式运算时不能有空格. 常用算术运算符号: ...

  4. 第2章 C语言的关系与逻辑运算符 (九)

    文章目录 文档视频讲解链接地址 2.11 关系与逻辑运算符 文档视频讲解链接地址 腾讯课堂视频链接 : 11_表达式_关系与逻辑运算符 2.11 关系与逻辑运算符 关系运算符如下: 关系运算符实例 i ...

  5. python怎么表示不等于_Python关系运算符中表示“不等于”的是哪个?________

    [单选题]下列不是Python语言关键字的是________ [多选题]Python的数字类型包括( ) [多选题]Python支持的数据类型有( ) [单选题]下面________不是Python合 ...

  6. matlab逻辑运算符用法,matlab的逻辑运算符

    Matlab 中的逻辑运算"&&"与"&","||"与"|"的区别 (2009-12-06 20 ...

  7. python画代码-Python教程_Python画Mandelbrot集 代码

    Python教程_Python画Mandelbrot集 代码 作者:Comet 来源: 课课家 www.kokojia.com点击数:278发布时间:2015-06-19 11:17:19 曼德勃罗集 ...

  8. 计算机python教程_Python 如何入门?附Python教程下载

    学习编程,有兴趣最好,小时候就开始捣鼓电脑.知识兔上有大量编程视频教程课程,都是精品课程,1080P超高清画质教学视频,精品之精品,找一套优秀Python课程教程或者找公众号超乎想象客服推荐一下,轻轻 ...

  9. 在网上购物竟然也能和Python扯上关系!电脑上用自己写的程序购物?别说,QT实现还真挺简单(Python如何利用QT制作电脑的手机版拼多多)

    拼多多现在也是越来越火了,但是不知道大家有没有发现,拼多多似乎一直不能通过网页进行访问.淘宝,京东都有微信客户端,那么怎么拼多多就没有呢?带着这个疑问,我特地去手机爬取了一下拼多多的客户端.(其实也不 ...

最新文章

  1. 使用slice和concat对数组的深拷贝和浅拷贝
  2. 吴恩达深度学习笔记(109)-循环神经网络模型(RNN介绍)
  3. 图解丨卷积神经网络数学原理解析
  4. 学习如何面对失败比成功更重要
  5. JS对象和JSON字符串相互转化总结
  6. 软件本地化,软件本地化公司
  7. 白话Elasticsearch18-深度探秘搜索技术之基于slop参数实现近似匹配以及原理剖析
  8. notepad++中的unexpected indent
  9. javascript一些常用的代码
  10. python闭包两种写法_浅析Python闭包
  11. 数据结构预算法(六) 数组和矩阵(1)
  12. 传奇服务器怎么修改升级武器成功,传奇服务端中设置装备元素升级不会破碎教程...
  13. siv技嘉硬件Linux,Gigabyte技嘉System Information Viewer(SIV)风扇控制软件B20.0529.1版For Win10-64(2020年6月16日发布)...
  14. ZigBee Dotdot
  15. 项目常用的合同类型,特点及报价方式
  16. 计算机专业 大一课程,计算机专业大一课程
  17. 机构视角筛选底部平台突破股票
  18. 笔记本电脑wlan+开启移动热点+手机转圈圈【已解决】
  19. 浅谈cpu、缓存、内存之间的关系
  20. 炒股小白入门知识——黄金交叉与死亡交叉

热门文章

  1. AxMath pj 并添加到WPS
  2. 2021中国大数据50强震撼揭晓 智领云再度荣登榜单!
  3. 软考:计算机技术与软件专业技术资格(水平)考试
  4. 桔子浏览器抢票专版官方版
  5. python中怎样使用re模块_PYTHON正则表达式 re模块使用说明
  6. imperva-waf配置 指定url禁止访问
  7. TQ2440使用手册
  8. 鲲鹏HCIA学习笔记【一】鲲鹏体系介绍
  9. golang xorm mysql_golang 之xorm
  10. 目前最快,视频剪辑批量制作字幕方法,剪映str字幕提取!