Python3 operators



What is an operator?

This chapter mainly explains Python’s operators. Let’s take a simple example 4 +5 = 9 . In the example, 4 and 5 are called operands, and "+" is called the operator.

The Python language supports the following types of operators:

  • Arithmetic operators

  • Comparison (relational) operators

  • Assignment operator

  • Logical operator

  • Bitwise operator

  • Member operator

  • Identity operator

  • Operator precedence

Next let us learn Python operators one by one.


Python arithmetic operators

The following assumes that variable a is 10 and variable b is 21:

##%%**//The following example demonstrates the operation of all arithmetic operators in Python:
OperatorDescriptionExample
+Add - Add two objectsa + b Output result 31
-Subtract - Get a negative number or one number minus another numbera - b Output result-11
*Multiplication - Multiply two numbers or return a string repeated several timesa * b Output result 210
/Division - x divides by yb / a Output result 2.1
## Modulo - Returns the remainder of division b % a Output result 1
Power - Returns the y power of x a**b is 10 raised to the 21st power
Take integer division - return the integer part of the quotient9// 2 Output result 4, 9.0//2.0 Output result 4.0
#!/usr/bin/python3

a = 21
b = 10
c = 0

c = a + b
print ("1 - c 的值为:", c)

c = a - b
print ("2 - c 的值为:", c)

c = a * b
print ("3 - c 的值为:", c)

c = a / b
print ("4 - c 的值为:", c)

c = a % b
print ("5 - c 的值为:", c)

# 修改变量 a 、b 、c
a = 2
b = 3
c = a**b 
print ("6 - c 的值为:", c)

a = 10
b = 5
c = a//b 
print ("7 - c 的值为:", c)

Output result of the above example :

1 - c 的值为: 31
2 - c 的值为: 11
3 - c 的值为: 210
4 - c 的值为: 2.1
5 - c 的值为: 1
6 - c 的值为: 8
7 - c 的值为: 2

Python comparison operator

The following assumes that variable a is 10 and variable b is 20:

Operator ==!=><>=<=

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

#!/usr/bin/python3

a = 21
b = 10
c = 0

if ( a == b ):
   print ("1 - a 等于 b")
else:
   print ("1 - a 不等于 b")

if ( a != b ):
   print ("2 - a 不等于 b")
else:
   print ("2 - a 等于 b")

if ( a < b ):
   print ("3 - a 小于 b")
else:
   print ("3 - a 大于等于 b")

if ( a > b ):
   print ("4 - a 大于 b")
else:
   print ("4 - a 小于等于 b")

# 修改变量 a 和 b 的值
a = 5;
b = 20;
if ( a <= b ):
   print ("5 - a 小于等于 b")
else:
   print ("5 - a 大于  b")

if ( b >= a ):
   print ("6 - b 大于等于 b")
else:
   print ("6 - b 小于 b")

The output results of the above examples:

1 - a 不等于 b
2 - a 不等于 b
3 - a 大于等于 b
4 - a 大于 b
5 - a 小于等于 b
6 - b 大于等于 b

Python assignment operators

The following hypothetical variables a is 10, variable b is 20:

DescriptionInstance
Equal - Compare objects for equality(a == b) Return False.
Not equal to - Compares whether two objects are not equal(a != b) Returns true.
Greater than - Returns whether x is greater than y(a > b) Returns False.
Less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. These are equivalent to the special variables True and False respectively. Note the capitalization of these variable names. (a < b) returns true.
Greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) Returns False.
Less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns true.
OperatorDescriptionInstance
=Simple assignment operatorc = a + b assigns the operation result of a + b to c
+=Additional assignment operatorc += a is equivalent to c = c + a
-=Subtractive assignment operatorc -= a is equivalent to c = c - a
*=Multiplicative assignment operatorc *= a Equivalent to c = c * a
/=Division assignment operatorc /= a Equivalent to c = c / a
%=Modulo assignment operatorc %= a is equivalent to c = c % a
**=Power assignment operatorc **= a is equivalent to c = c ** a
//=Take integer division assignment operatorc //= a is equivalent to c = c // a

The following example demonstrates all of Python Operation of assignment operator:

#!/usr/bin/python3

a = 21
b = 10
c = 0

c = a + b
print ("1 - c 的值为:", c)

c += a
print ("2 - c 的值为:", c)

c *= a
print ("3 - c 的值为:", c)

c /= a 
print ("4 - c 的值为:", c)

c = 2
c %= a
print ("5 - c 的值为:", c)

c **= a
print ("6 - c 的值为:", c)

c //= a
print ("7 - c 的值为:", c)

Output result of the above example:

1 - c 的值为: 31
2 - c 的值为: 52
3 - c 的值为: 1092
4 - c 的值为: 52.0
5 - c 的值为: 2
6 - c 的值为: 2097152
7 - c 的值为: 99864

Python bitwise operator

Bitwise operator treats numbers as binary. computational. The bitwise operation rules in Python are as follows:

In the following table, variable a is 60 and b is 13.

##|Bitwise OR operator: As long as one of the two corresponding binary bits is 1, the result bit will be 1. (a | b) The output result is 61, binary interpretation: 0011 1101##^~##<<Left shift operator: Each binary digit of the operand is shifted to the left by a certain number of bits, specified by the number on the right of "<<" The number of bits to move, the high bits are discarded, and the low bits are filled with 0s. a << 2 The output result is 240, binary interpretation: 1111 0000>>Right shift operator: " >>"All binary digits of the operand on the left are shifted to the right by a certain number of digits,">>"The number on the right specifies the number of digits to movea >> 2 The output result is 15, binary Explanation: 0000 1111

The following examples demonstrate the operations of all bitwise operators in Python:

#!/usr/bin/python3

a = 60            # 60 = 0011 1100 
b = 13            # 13 = 0000 1101 
c = 0

c = a & b;        # 12 = 0000 1100
print ("1 - c 的值为:", c)

c = a | b;        # 61 = 0011 1101 
print ("2 - c 的值为:", c)

c = a ^ b;        # 49 = 0011 0001
print ("3 - c 的值为:", c)

c = ~a;           # -61 = 1100 0011
print ("4 - c 的值为:", c)

c = a << 2;       # 240 = 1111 0000
print ("5 - c 的值为:", c)

c = a >> 2;       # 15 = 0000 1111
print ("6 - c 的值为:", c)

The output results of the above examples:

1 - c 的值为: 12
2 - c 的值为: 61
3 - c 的值为: 49
4 - c 的值为: -61
5 - c 的值为: 240
6 - c 的值为: 15

Python logical operators

Python language support Logical operator, the following assumes that variable a is 10 and b is 20:

OperatorDescriptionInstance
& Bitwise AND operator: Two values ​​participating in the operation, if the two corresponding bits are 1, the result of the bit is 1, otherwise it is 0(a & b) The output result is 12, binary interpretation : 0000 1100
Bitwise XOR operator: when two corresponding When the binary bits are different, the result is 1(a ^ b) and the output result is 49. Binary interpretation: 0011 0001
bitwise Negation operator: Negate each binary bit of the data, that is, change 1 to 0, and change 0 to 1(~a) The output result is -61, binary interpretation: 1100 0011, in a The two's complement form of a signed binary number.
OperatorLogical expressionDescriptionExample
andx and yBoolean "AND" - if x is False, x and y return 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) returns 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")

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

Python 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 .

OperatorDescriptionInstance
in 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 中")

The output result of the above example:

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

Python Identity operator

Identity operator is used to compare the storage locations of two objects

OperatorDescriptionExample
isis is to determine whether two identifiers are referenced from an objectx is y, if id(x) is equal to id(y) , is returns result 1
is notis not is used to determine whether the two identifiers refer to different objectsx is not y, if id(x) is not equal to id(y). is not Return result 1

The following examples demonstrate 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 有相同的标识")

The output results of the above examples:

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

Python operator priority

The following table All operators are listed from highest to lowest precedence:

##* / % //Multiplication, division, modulo and integer division+ -Addition and subtraction>> <<Right shift, left shift operator&bit'AND'^ |bit operator<= < > >=Comparison operator<> == !=Equals Operator##= %= /= //= -= += *= **=is is notin not in not or andThe following example demonstrates the operation of all operator precedence in Python:
OperatorDescription
**Exponent (highest priority)
~ + -bitwise flip, unary plus and minus signs (the last two The method names are +@ and -@)
Assignment operator
Identity Operator
Membership Operator
Logical operators
#!/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)

The above example output Result:

(a + b) * c / d 运算结果为: 90.0
((a + b) * c) / d 运算结果为: 90.0
(a + b) * (c / d) 运算结果为: 90.0
a + (b * c) / d 运算结果为: 50.0