Python3 operator


What is an operator?

This section focuses on Python's operators. T o give a simple example, 4 plus 5 plus 9. In the example, 4 and 5 are called operans, and + are called operators.

The Python language supports the following types of operators:

Let's learn the operators of Python one by one.


Python arithmetic operator

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

Operator Describe Instance
+ Plus - Two objects add up A-b output 31
- Subtract - Get a negative number or one number minus another number a - b Output -11
* Multiply - Multiply two numbers or return a string that is repeated several times a :b Output 210
/ Divide - x divided by y b / a Output 2.1
% Molding - Returns the remaining part of the divide b % a Output 1
** Power - Returns the y-power of x 21th party with a b of 10
// Rounding and divide - returns the integer portion of the dealer 9//2 Output 4, 9.0//2.0 Output 4.0

The following example demonstrates the operation of all Python arithmetic operators:

#!/usr/bin/python3
#coding=utf-8

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)

The above example output results:

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 the variable a is 10 and the variable b is 20:

Operator Describe Instance
== Equal to -- Compare whether an object is equal False is returned.
!= Not equal to -- Compare whether two objects are not equal (a!-b) returns True.
> Greater than -- returns whether x is greater than y (a) Return false.
< Less than -- returns whether x is less than y. A ll comparison operators return 1 for true and 0 for false. T his is equivalent to special variables True and False, respectively. Note that the names of these variables are capitaled. (a- slt; b) returns True.
>= Greater than or equal to -- returns whether x is greater than or equal to y. (a) return false.
<= Less than or equal to -- returns whether x is less than or equal to y. (a slt;?b) returns True.

The following example demonstrates the operation of all of Python's comparison operators:

#!/usr/bin/python3
#coding=utf-8

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 above example output results:

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

Python assignment operator

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

Operator Describe Instance
= A simple assignment operator C s a s b assigns the result of the operation of a sb to c
+= The addition assignment operator C s a is equivalent to c s c s a
-= Subtract the assignment operator c --a is equivalent to c-c-a
*= Multiplication assignment operator c s a is equivalent to c s c s a
/= Division assignment operator c /-a is equivalent to c-c/a
%= The mold assignment operator c %=a is equivalent to c=c %a
**= Power assignment operator c s a is equivalent to c s c s a
//= Round and divide the assignment operator c //-a is equivalent to c-c /a

The following example demonstrates the operation of all Python assignment operators:

#!/usr/bin/python3
#coding=utf-8

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)

The above example output results:

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

Python bit operator

Bit operators are calculated by treating numbers as binary. The bit-by-bit algorithm in Python is as follows:

The variables a in the following table are 60 and b is 13.

Operator Describe Instance
& Bit by operator: The two values that participate in the operation, and if both of the corresponding bits are 1, the result of that bit is 1, otherwise 0 (a and b) Output result 12, binary explanation: 0000 1100
| By bit or operator: The result bit is 1 as long as one of the corresponding two binary bits is 1. (a| b) Output 61, binary explanation: 0011 1101
^ By bit or operator: When two corresponding bits are different, the result is 1 Output result 49, binary explanation: 0011 0001
~ Inverse operator by bit: Reverses each binary bit of data, i.e. changes 1 to 0 and 0 to 1 (-a) Output -61, Binary Explanation: 1100 0011, in the form of a complement to a signed binary number.
<< Left move operator: Each two-digit of the number of operations all move several bits to the left, by the number on the right of the "lt;"" specifies the number of moving digits, high drop, low fill 0. a .lt;2 Output 240, Binary Explanation: 1111 0000
>> Right-hand move operator: Move all two bits of the number of operations on the left of the " a .gt;2 Output Result 15, Binary Explanation: 0000 1111

The following example demonstrates the operation of all Python bit operators:

#!/usr/bin/python3
#coding=utf-8

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 above example output results:

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

Python logical operator

The Python language supports logical operators, assuming that the variables a are 10 and b is 20:

Operator Logical expression Describe Instance
and x and y Boolean "and" - If x is False, x and y return False, otherwise it returns the calculated value of y. (a and b) return 20.
or x or y Boolean "or" - If x is True, it returns the value of x, otherwise it returns the calculated value of y. (a or b) returns 10.
not not x Boolean "no" - If x is True, false is returned. If x is False, it returns True. Not (a and b) returns False

The above example output results:

#!/usr/bin/python3
#coding=utf-8

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 above example output results:

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 supports member operators, and the test instance contains a series of members, including strings, lists, or futons.

Operator Describe Instance
in True if a value is found in the specified sequence, otherwise False is returned. x in the y sequence, if x returns true in the y sequence.
not in True if no value is found in the specified sequence, otherwise False is returned. x is not in the y sequence, if x is not in the y sequence true.

The following example demonstrates the operation of all Python member operators:

#!/usr/bin/python3
#coding=utf-8
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 above example output results:

1 - 变量 a 不在给定的列表中 list2 - 变量 b 不在给定的列表中 list3 - 变量 a 在给定的列表中 list

Python identity operator

Identity operators are used to compare storage units for two objects

Operator Describe Instance
is is is to determine whether two identifiers are referenced from one object x is y, if id(x) is equal to id(y), is returns the result true
is not is not is to determine whether two identifiers are referenced from different objects x is not y, if id(x) is not equal to id(y). Is not returns the result true

The following example demonstrates the operation of all Python identity operators:

#!/usr/bin/python3
#coding=utf-8

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 above example output results:

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

Python operator priority

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

Operator Describe
** Index (highest priority)
~ + - Flip by bit, dollar plus sign and minus sign (the last two methods are named s and - s)
* / % // Multiply, divide, mold and round
+ - Addition and subtract
>> << Move right, move left operator
& Bit 'AND'
^ | Bit operator
<= < > >= Comparison operator
<> == != Comparison operator
= %= /= //= -= += *= **= The assignment operator
is is not Identity operator
in not in The member operator
not or and The logical operator

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

#!/usr/bin/python3
#coding=utf-8
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 results:

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