Python Operators
What is an Operator?
This chapter mainly explains Python operators. Let's take a simple example 4 +5 = 9 In the example,4and5is calledoperand,+is called an operator.
The Python language supports the following types of operators:
- Arithmetic operators
- Comparison (relational) operators
- Assignment operators
- Logical operators
- Bitwise operators
- Membership operators
- Identity operators
- Operator precedence
Next, let's learn Python operators one by one.
Python Arithmetic Operators
The following assumes variables: a=10,b=20:
| Operator | Description | Example |
|---|---|---|
| + | Addition - Adds two objects | a + b outputs 30 |
| - | Subtraction - Gives a negative number or subtracts one number from another | a - b outputs -10 |
| * | Multiplication - Multiplies two numbers or returns a string repeated a number of times | a * b outputs 200 |
| / | Division - x divided by y | b / a outputs 2 |
| % | Modulus - Returns the remainder of division | b % a outputs 0 |
| ** | Exponent - Returns x raised to the power y | a**b is 10 to the 20th power, outputs 100000000000000000000 |
| // | Floor division - Returns the integer part of the quotient (rounded down) |
>>> 9//2 4 >>> -9//2 -5 |
The following example demonstrates the operations of all Python arithmetic operators:
Examples (Python 2.0+)
Run example »
The output of the above example is:
1 - c 的值为: 31 2 - c 的值为: 11 3 - c 的值为: 210 4 - c 的值为: 2 5 - c 的值为: 1 6 - c 的值为: 8 7 - c 的值为: 2
Note:In Python 2.x, integer divided by integer can only yield an integer. If you want the fractional part, change one of the numbers to a float.
>>> 1/2 0 >>> 1.0/2 0.5 >>> 1/float(2) 0.5
Python Comparison Operators
The following assumes variable a is 10 and variable b is 20:
| Operator | Description | Example |
|---|---|---|
| == | Equals - Compares whether objects are equal | (a == b) returns False. |
| != | Not equal - Compares whether two objects are not equal | (a != b) returns True. |
| <> | Not equal - Compares whether two objects are not equal.Deprecated in Python 3. | (a <> b) returns True. This operator is similar to !=. |
| > | 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. | (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. |
The following example demonstrates the operations of all Python comparison operators:
Examples (Python 2.0+)
The output of the above example is:
1 - a 不等于 b 2 - a 不等于 b 3 - a 不等于 b 4 - a 大于等于 b 5 - a 大于 b 6 - a 小于等于 b 7 - b 大于等于 a
Python Assignment Operators
The following assumes variable a is 10 and variable b is 20:
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment operator | c = a + b assigns the result of a + b to c |
| += | Addition assignment operator | c += a is equivalent to c = c + a |
| -= | Subtraction assignment operator | c -= a is equivalent to c = c - a |
| *= | Multiplication assignment operator | c *= a is equivalent to c = c * a |
| /= | Division assignment operator | c /= a is equivalent to c = c / a |
| %= | Modulus assignment operator | c %= a is equivalent to c = c % a |
| **= | Exponent assignment operator | c **= a is equivalent to c = c ** a |
| //= | Floor division assignment operator | c //= a is equivalent to c = c // a |
The following example demonstrates the operations of all Python assignment operators:
Examples (Python 2.0+)
The output of the above example is:
1 - c 的值为: 31 2 - c 的值为: 52 3 - c 的值为: 1092 4 - c 的值为: 52 5 - c 的值为: 2 6 - c 的值为: 2097152 7 - c 的值为: 99864
Python Bitwise Operators
Bitwise operators treat numbers as binary for calculation. The bitwise operation rules in Python are as follows:
In the table below, variable a is 60 and b is 13, with binary formats as follows:
a = 0011 1100 b = 0000 1101 ----------------- a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND operator: For the two values involved in the operation, if both corresponding bits are 1, the result bit is 1; otherwise, it is 0. | (a & b) outputs 12, binary explanation: 0000 1100 |
| | | Bitwise OR operator: As long as one of the two corresponding binary bits is 1, the result bit is 1. | (a | b) outputs 61, binary explanation: 0011 1101 |
| ^ | Bitwise XOR operator: When the two corresponding binary bits are different, the result is 1. | (a ^ b) outputs 49, binary explanation: 0011 0001 |
| ~ | Bitwise NOT operator: Inverts each bit in the binary representation, changing 0 to 1 and 1 to 0.~xSimilar to-x-1 | (~a ) outputs -61, binary explanation: 1100 0011 (represented in two's complement form), in the two's complement form of a signed binary number. |
| << | Left shift operator: All binary bits of the operand are shifted left by a number of bits, determined by<<The number on the right specifies the number of positions to shift. High-order bits are discarded, and low-order bits are filled with 0. | a << 2 outputs 240, binary explanation: 1111 0000 |
| >> | Right shift operator: shifts all binary bits of the operand on the left of ">>" to the right by the specified number of places,>>The number on the right specifies the number of positions to shift. | a >> 2 outputs 15, binary explanation: 0000 1111 |
The following example demonstrates the operations of all bitwise operators in Python:
Examples (Python 2.0+)
The output of the above example is:
1 - c 的值为: 12 2 - c 的值为: 61 3 - c 的值为: 49 4 - c 的值为: -61 5 - c 的值为: 240 6 - c 的值为: 15
Python Logical Operators
Python language supports logical operators. Assume variable a is 10 and b is 20:
| Operator | Logical expression | Description | Example |
|---|---|---|---|
| and | x and y | Boolean "and" - If x is False, x and y returns False, otherwise it returns the evaluated value of y. | (a and b) returns 20. |
| or | x or y | Boolean "or" - If x is non-zero, it returns the evaluated value of x, otherwise it returns the evaluated 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 of the above example is:
Examples (Python 2.0+)
The output of the above example is:
1 - 变量 a 和 b 都为 True 2 - 变量 a 和 b 都为 True,或其中一个变量为 True 3 - 变量 a 和 b 有一个不为 True 4 - 变量 a 和 b 都为 True,或其中一个变量为 True 5 - 变量 a 和 b 都为 False,或其中一个变量为 False
Python Membership Operators
In addition to the operators above, Python also supports membership operators, which test whether a sequence contains a series of members, including strings, lists, or tuples.
| Operator | Description | Example |
|---|---|---|
| in | Returns True if the value is found in the specified sequence, otherwise returns False. | x in 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, otherwise returns False. | x not in y sequence, returns True if x is not in the y sequence. |
The following example demonstrates the operations of all membership operators in Python:
Examples (Python 2.0+)
The output of the above example is:
1 - 变量 a 不在给定的列表中 list 中 2 - 变量 b 不在给定的列表中 list 中 3 - 变量 a 在给定的列表中 list 中
Python Identity Operators
Identity operators are used to compare the storage units of two objects
| Operator | Description | Example |
|---|---|---|
| is | is determines whether two identifiers reference the same object | x is y, similar toid(x) == id(y), if they reference the same object, returns True; otherwise returns False |
| is not | is not determines whether two identifiers reference different objects | x is not y, similar toid(a) != id(b). If they do not reference the same object, the result returns True; otherwise returns False. |
Note: id()The function is used to get the memory address of an object.
The following example demonstrates the operations of all identity operators in Python:
Examples (Python 2.0+)
The output of the above example is:
1 - a 和 b 有相同的标识 2 - a 和 b 有相同的标识 3 - a 和 b 没有相同的标识 4 - a 和 b 没有相同的标识
Difference between is and ==:
is is used to determine whether two variable references refer to the same object (the same memory space), == is used to determine whether the values of the referenced 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:
| Operator | Description |
|---|---|
| ** | Exponentiation (highest precedence) |
| ~ + - | Bitwise inversion, unary plus and minus (the method names for the last two are +@ and -@) |
| * / % // | Multiply, divide, modulo and floor division |
| + - | Addition and subtraction |
| >> << | Right shift, left shift operators |
| & | Bitwise 'AND' |
| ^ | | Bitwise operators |
| <= < > >= | Comparison operators |
| <> == != | Equality operators |
| = %= /= //= -= += *= **= | Assignment operators |
| is is not | Identity operators |
| in not in | Membership operators |
| not and or | Logical operators |
The following example demonstrates the operations of all operator precedence in Python:
Examples (Python 2.0+)
The output of the above example is:
(a + b) * c / d 运算结果为: 90 ((a + b) * c) / d 运算结果为: 90 (a + b) * (c / d) 运算结果为: 90 a + (b * c) / d 运算结果为: 50Other extensions