python 逻辑运算符_Python关系和逻辑运算符

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,java,c++,编程语言,linux)