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:
Operator | Description | Example |
---|---|---|
+ | Add - Add two objects | a + b Output result 31 |
- | Subtract - Get a negative number or one number minus another number | a - b Output result-11 |
* | Multiplication - Multiply two numbers or return a string repeated several times | a * b Output result 210 |
/ | Division - x divides by y | b / 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 quotient | 9// 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 的值为: 2Python comparison operator
The following assumes that variable a is 10 and variable b is 20:
Description | Instance | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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. |
Operator | Description | Instance |
---|---|---|
= | Simple assignment operator | c = a + b assigns the operation result of a + b to c |
+= | Additional assignment operator | c += a is equivalent to c = c + a |
-= | Subtractive assignment operator | c -= a is equivalent to c = c - a |
*= | Multiplicative assignment operator | c *= a Equivalent to c = c * a |
/= | Division assignment operator | c /= a Equivalent to c = c / a |
%= | Modulo assignment operator | c %= a is equivalent to c = c % a |
**= | Power assignment operator | c **= a is equivalent to c = c ** a |
//= | Take integer division assignment operator | c //= 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.
Operator | Description | Instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
& | 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 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. | ##<< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
a << 2 The output result is 240, binary interpretation: 1111 0000 | >> | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
a >> 2 The output result is 15, binary Explanation: 0000 1111 |
Operator | Logical expression | Description | Example |
---|---|---|---|
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) returns 20. |
or | x or y | Boolean "or" - if x is True, it returns True, otherwise it returns the calculated value of y. | (a or b) returns 10. |
not | not x | Boolean "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 .
Operator | Description | Instance |
---|---|---|
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
Operator | Description | Example |
---|---|---|
is | is is to determine whether two identifiers are referenced from an object | x is y, if id(x) is equal to id(y) , is returns result 1 |
is not | is not is used to determine whether the two identifiers refer to different objects | x 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:
Operator | Description |
---|---|
** | Exponent (highest priority) |
~ + - | bitwise flip, unary plus and minus signs (the last two The method names are +@ and -@) |
Multiplication, division, modulo and integer division | |
Addition and subtraction | |
Right shift, left shift operator | |
bit'AND' | |
bit operator | |
Comparison operator | |
Equals Operator | |
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