Home > Backend Development > Python Tutorial > Python basic learning logical operators, member operators, operator priority

Python basic learning logical operators, member operators, operator priority

little bottle
Release: 2019-04-27 15:23:25
Original
4012 people have browsed it

Connecting the previous two articles, this article will continue to tell you about the basic learning of Python's logical operators, member operators and operator precedence. It has high learning value and is interesting. friends to find out.

Logical operators

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

##Operator Logical expressionDescriptionInstanceandx and yBoolean "AND" - If x is False, x and y returns False, otherwise it returns the calculated value of y. (a and b) returns 20. orx or yBoolean "or" - if x is True, it returns True, otherwise it returns the calculated value of y. (a or b) returns 10. notnot xBoolean "not" - If x is True, returns False. If x is False, it returns True. not(a and b) Return False
The output result of the above example:

#!/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")
Copy after login

The output result of the above example:

1 - 变量 a 和 b 都为 true
2 - 变量 a 和 b 都为 true,或其中一个变量为 true
3 - 变量 a 和 b 有一个不为 true
4 - 变量 a 和 b 都为 true,或其中一个变量为 true
5 - 变量 a 和 b 都为 false,或其中一个变量为 false
Copy after login

Member Operator

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

OperatorDescriptionInstancein Returns True if a value is found in the specified sequence, False otherwise. x is in the y sequence, returns True if x is in the y sequence. not in Returns True if the value is not found in the specified sequence, False otherwise. x is not in the y sequence, returns True if x is not in the y sequence.
The following example demonstrates the operation of all member operators in Python:

#!/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 中")
Copy after login

The output result of the above example is:

1 - 变量 a 不在给定的列表中 list 中
2 - 变量 b 不在给定的列表中 list 中
3 - 变量 a 在给定的列表中 list 中
Copy after login

The identity operator is used Storage location for comparing two objects

OperatorDescriptionExampleisis is used to determine whether two identifiers refer to an object. x is y, if id(x) is equal to id(y), is notis not is to determine whether the two identifiers refer to different objects. x is not y, if id( x) is not equal to id(y).
is returns Result 1
is not Return result 1
The following example demonstrates the operation of all identity operators in Python:

#!/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 有相同的标识")
Copy after login

The above example output result:

1 - a 和 b 有相同的标识
2 - a 和 b 有相同的标识
3 - a 和 b 没有相同的标识
4 - a 和 b 没有相同的标识
Copy after login

Operator priority

The following table lists all operators from the highest to the lowest priority:

OperatorDescription**Index (highest precedence)~ -Bitwise flip, unary plus sign and minus sign (the last two methods are named @ and -@)* / % //Multiplication, division, modulo and integer division -Addition and subtraction## >> <<&^ |##<= < > >=Comparison operators<> == !=Equal operator= %= /= //= -= = *= **=Assignment operatoris is notIdentity operatorMember operatorLogical operatorThe following example demonstrates the operation of all operator priorities in Python:
#!/usr/bin/python3
a = 20
b = 10
c = 15
d = 5
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)
Copy after login
The output result of the above example:
Right shift, left shift operator
bit'AND'
Bitwise operators
## in not in
not or and
(a + b) * c / d 运算结果为: 90.0
((a + b) * c) / d 运算结果为: 90.0
(a + b) * (c / d) 运算结果为: 90.0
a + (b * c) / d 运算结果为: 50.0
Copy after login

Related tutorials:

Python video tutorial

The above is the detailed content of Python basic learning logical operators, member operators, operator priority. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template