Python Operators (2)

Python logical operators

The Python language supports logical operators. The following assumes that variable a is 10 and b is 20:

operator logical expression describe example
and x and y Boolean AND - x and y returns the value of x if x is False, otherwise returns the computed value of y. (a and b) returns 20.
or x or y Boolean OR - If x is True, it returns the value of x, otherwise it returns the computed value of y. (a or b) Return 10.
not not x Boolean "not" - Returns False if x is True. It returns True if x is False. not(a and b) returns False

The following examples demonstrate the operation of all logical operators in Python:

Example (Python 3.0+)

#!/usr/bin/python3
 
a = 10
b = 20
 
if ( a and b ):
   print ("1 - 变量 a 和 b 都为 true")
else:
   print ("1 - 变量 a 和 b 有一个不为 true")
 
if ( a or b ):
   print ("2 - 变量 a 和 b 都为 true,或其中一个变量为 true")
else:
   print ("2 - 变量 a 和 b 都不为 true")
 
# 修改变量 a 的值
a = 0
if ( a and b ):
   print ("3 - 变量 a 和 b 都为 true")
else:
   print ("3 - 变量 a 和 b 有一个不为 true")
 
if ( a or b ):
   print ("4 - 变量 a 和 b 都为 true,或其中一个变量为 true")
else:
   print ("4 - 变量 a 和 b 都不为 true")
 
if not( a and b ):
   print ("5 - 变量 a 和 b 都为 false,或其中一个变量为 false")
else:
   print ("5 - 变量 a 和 b 都为 true")

The output of the above example is shown in the figure below:

insert image description here

Python membership operator

In addition to some of the above operators, Python also supports member operators, and the test instance contains a series of members, including strings, lists or tuples.

operator describe example
in Returns True if the value is found in the specified sequence, otherwise returns False. x is in the y sequence, returns True if x is in the y sequence.
not in Returns True if no value is found in the specified sequence, otherwise returns False. x is not in the y sequence, returns True if x is not in the y sequence.

The following examples demonstrate the operation of all Python membership operators:

Example (Python 3.0+)

#!/usr/bin/python3
 
a = 10
b = 20
list = [1, 2, 3, 4, 5 ]
 
if ( a in list ):
   print ("1 - 变量 a 在给定的列表中 list 中")
else:
   print ("1 - 变量 a 不在给定的列表中 list 中")
 
if ( b not in list ):
   print ("2 - 变量 b 不在给定的列表中 list 中")
else:
   print ("2 - 变量 b 在给定的列表中 list 中")
 
# 修改变量 a 的值
a = 2
if ( a in list ):
   print ("3 - 变量 a 在给定的列表中 list 中")
else:
   print ("3 - 变量 a 不在给定的列表中 list 中")

The output of the above example is shown in the figure below:

insert image description here

Python identity operator

The identity operator is used to compare the memory locations of two objects

operator describe example
is is is to judge whether two identifiers refer to an object x is y , like id(x) == id(y) , returns True if they refer to the same object, otherwise returns False
is not is not is to judge whether two identifiers refer to different objects x is not y , like id(x) != id(y) . Returns True if the reference is not the same object, False otherwise.

Note: The id() function is used to obtain the memory address of the object.

The following examples demonstrate the operation of all of Python's identity operators:

Example (Python 3.0+)

#!/usr/bin/python3
 
a = 20
b = 20
 
if ( a is b ):
   print ("1 - a 和 b 有相同的标识")
else:
   print ("1 - a 和 b 没有相同的标识")
 
if ( id(a) == id(b) ):
   print ("2 - a 和 b 有相同的标识")
else:
   print ("2 - a 和 b 没有相同的标识")
 
# 修改变量 b 的值
b = 30
if ( a is b ):
   print ("3 - a 和 b 有相同的标识")
else:
   print ("3 - a 和 b 没有相同的标识")
 
if ( a is not b ):
   print ("4 - a 和 b 没有相同的标识")
else:
   print ("4 - a 和 b 有相同的标识")

The output of the above example is shown in the figure below:

insert image description here

The difference between is and ==:
is is used to judge whether the reference objects of two variables are the same, and == is used to judge whether the values ​​of reference variables are equal.

>>>a = [1, 2, 3]
>>> b = a
>>> b is a 
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True

Python operator precedence

The following table lists all operators from highest to lowest precedence, with operators within the same cell having the same precedence. Operators refer to binary operations, unless otherwise noted. Operators within the same cell are grouped from left to right (except that exponentiation is grouped from right to left):

operator

describe

(expressions...),

[expressions...], {key: value...}, {expressions...}

parenthesized expressions

x[index], x[index:index], x(arguments...), x.attribute

read, slice, call, property reference

await x

await expression

**

power (exponent)

+x, -x, ~x

Positive, Negative, Bitwise NOT

*, @, /, //, %

multiply, matrix multiply, divide, divisible, remainder

+, -

add and subtract

<<,>>

shift

&

bitwise AND

^

bitwise XOR

|

bitwise OR

in,not in, is,is not, <, <=, >, >=, !=, ==

Comparison operations, including membership checks and identification number checks

not x

logical NOT

and

Logic and AND

or

logical OR

if -- else

conditional expression

lambda

lambda expression

:=

assignment expression

The following example demonstrates the operation of all Python operator precedence:

Example (Python 3.0+)

 #!/usr/bin/python3

a = 20
b = 10
c = 15
d = 5![在这里插入图片描述](https://img-blog.csdnimg.cn/46f4664c002644fbb1453f2bd19d346d.png#pic_center)

e = 0

e = (a + b) * c / d       #( 30 * 15 ) / 5
print ("(a + b) * c / d 运算结果为:",  e)

e = ((a + b) * c) / d     # (30 * 15 ) / 5
print ("((a + b) * c) / d 运算结果为:",  e)

e = (a + b) * (c / d)    # (30) * (15/5)
print ("(a + b) * (c / d) 运算结果为:",  e)

e = a + (b * c) / d      #  20 + (150/5)
print ("a + (b * c) / d 运算结果为:",  e)

The output of the above example is shown in the figure below:

insert image description here

and has higher precedence:

example

x = True
y = False
z = False
 
if x or y and z:
    print("yes")
else:
    print("no")

The above example first calculates y and z and returns False, then x or False returns True, and the output result is shown in the figure below:

insert image description here
Note: Python3 does not support the <> operator, you can use != instead, if you must use this comparison operator, you can use the following method:

>>> from __future__ import barry_as_FLUFL
>>> 1 <> 2
True

postscript

After learning how to use Python operators, we can use Python to program more flexibly to realize various logics and operations. In practical applications, it is necessary to flexibly select and use different operators according to the actual situation in order to achieve the optimal effect. In addition, we also need to pay attention to the precedence and associativity of operators to avoid errors caused by operator precedence. When writing code, it is recommended to focus on the readability and maintainability of the code, and avoid using overly complex expressions and nested structures to facilitate subsequent code refactoring and maintenance.

Reprinted from: https://blog.csdn.net/u014727709/article/details/131622383
welcome to start, welcome to comment, welcome to correct

Guess you like

Origin blog.csdn.net/u014727709/article/details/131622383