Python3 Operators
What is an operator?
This section mainly describes the Python operators. For a simple example 4 +5 = 9 . In the example, 4 and 5 are called operands, and "+" are called operators.
The Python language supports the following types of operators:
- Arithmetic Operators
- Comparing (relational) operators
- Assignment Operator
- Logical Operators
- Bit Operator
- Member Operators
- Identity Operator
- Operator Priorities
Next let's learn Python operators one by one.
Python Arithmetic Operator
The following hypothesis variable a is 10 and variable b is 21:
Operator | Description | Instance |
---|---|---|
+ | Plus - Add two objects | a + b Output 31 |
- | minus - get a negative number or a number minus another number | a - b output result -11 |
* | multiply - multiply two numbers or return a string that is repeated several times | a * b output result 210 |
/ | Division - x divided by y | b / a Output 2.1s |
% | modulo - returns the remainder of the division | b % a Output 1 |
** | power - returns the power of y of x | a**b is the 21st power of 10 |
// | Division and division - Take an integer close to the divisor |
>>> 9//2 4 >>> -9//2 -5 |
The following example demonstrates the operation of all Python arithmetic operators.:
Instance(Python 3.0+)
The above example output:
1 - c Value: 31 2 - c Value: 11 3 - c Value: 210 4 - c Value: 2.1 5 - c Value: 1 6 - c Value: 8 7 - c Value: 2
Python comparison operator
The following hypothesis variable a is 10 and variable b is 20:
Operator | Description | Instance |
---|---|---|
== | is equal to - compare objects are equal | (a == b) returns False. |
!= | Not equal to - Compare 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. This is equivalent to the special variables True and False, respectively. Note that the capital names of these variables are capitalized. | (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 operation of all Python comparison operators:
Instance(Python 3.0+)
The output of the above example:
1 - a not equal to b 2 - a not equal to b 3 - a greater or equal to b 4 - a more than the b 5 - a Less than or equal to b 6 - b greater or equal to aa = 21 b = 10 c = 0
Python Assignment Operator
The following hypothesis variable a is 10 and variable b is 20:
Operator | Description | Instance |
---|---|---|
= | Simple assignment operator | c = a + b Assign the result of a + b to c |
+= | Additional 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 |
%= | Modification Assignment Operator | c %= a Equivalent to c = c % a |
**= | Power assignment operator | c **= a is equivalent to c = c ** a |
//= | Rounding the assignment operator | c //= a is equivalent to c = c // a |
The following example demonstrates the operation of all Python assignment operators:
Instance(Python 3.0+)
The above example output:
1 - c Value: 31 2 - c Value: 52 3 - c Value: 1092 4 - c Value: 52.0 5 - c Value: 2 6 - c Value: 2097152 7 - c Value: 99864
Python bit operator
The bitwise operator is calculated by treating the number as a binary. The bitwise algorithm in Python is as follows:
The variable a in the table below is 60, and b is 13 in binary format 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 | Instance |
---|---|---|
& | Bitwise AND Operator: Two values participating in the operation. If both corresponding bits are 1, the result of this bit is 1, otherwise 0 | < Td> (a & b) output 12, binary interpretation: 0000 1100|
| | Bitwise OR Operator: As long as one of the corresponding two binary digits is 1, the result bit is 1. | (a | b) Output 61, binary interpretation: 0011 1101 |
^ | bitwise XOR operator: When the two corresponding binary digits are different, the result is 1 | (a ^ b) Output 49, binary interpretation: 0011 0001 |
~ | Bitwise negation operator: Inverts each binary bit of data, that is, changes 1 to 0 and 0 to 1. ~x is similar to -x-1 | (~a ) output -61 , binary interpretation : 1100 0011, in the complement form of a signed binary number. |
<< | Left movement operator: Each binary of the operand is shifted left by several digits, and the number of digits moved by the number on the right side of "<<" is discarded. The low position is 0. | a << 2 output 240, binary interpretation: 1111 0000 |
>> | Right movement operator: shift all the binary digits of the operand to the left of ">>" to the right by several digits, and the number on the right side of ">>" Number of bits moved | a >> 2 Output result 15, binary interpretation: 0000 1111 |
Python Logic Operators
The Python language supports logical operators. The following assumes that the variable a is 10 and b is 20:
Operator | Logical Expression | Description | 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) returns 20. |
or | x or y | Boolean "or" - If x is True, it returns the value of x, otherwise it returns the computed value of y. | (a or b) returns 10. |
not | not x | Boolean "Non" - Returns 0 if x is True. If x is False, it returns True. | not(a and b) returns False |
The output of the above example:
Instance (Python 3.0+)
The output of the above example:
1 - Variables a and b are both true 2 - Variables a and b are both true, or one of the variables is true 3 - Variables a and b have one not true 4 - Variables a and b are both true, or one of the variables is true 5 - Variables a and b are both false, or one of the variables is false
Python member operator
In addition to some of the above operators, Python also supports member operators, which contain a series of members, including strings, lists, or tuples.
Operator | Description | Instance |
---|---|---|
in | Returns True if the value is found in the specified sequence, otherwise returns False. | x In the y sequence, if x returns True in the y sequence. |
not in | Returns True if no value is found in the specified sequence, otherwise returns False. | x is not in the y sequence, if x does not return True in the y sequence. |
The following example demonstrates the operation of all Python member operators:
Instance (Python 3.0+)
The output of the above example:
1 - variable a is not in the list in the given list 2 - variable b is not in the list in the given list 3 - variable a in the list in the given list
Python Identity Operator
The identity operator is used to compare the storage units of two objects
Operator | Description | Instance |
---|---|---|
is | Is is to determine whether the two identifiers are referenced from an object | x is y, similar to id(x) == id(y) , Returns True if the same object is referenced, otherwise returns False |
is not | is not is to determine if two identifiers are referenced from different objects | x is not y , similar to id (a) != id(b). Returns True if the reference is not the same object, otherwise returns False. |
The following example demonstrates the operation of all Python identity operators:
Instance (Python 3.0+)
The output of the above example:
1 - a and b have the same logo 2 - a and b have the same logo 3 - a and b do not have the same logo 4 - a and b do not have the same logo
is and == difference:
is is used to determine whether two variable reference objects are the same, and == is used to determine whether the values of the reference 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 Priority
The following table lists all operators from highest to lowest priority:
Operator | Description |
---|---|
** | index (highest priority) |
~ + - | Flip by bit, unary plus and minus (the last two methods are named +@ and -@) |
* / % // | multiply, divide, modulo and divide by |
+ - | addition subtraction |
>> << | Shift right, left shift operator |
& | bit 'AND' |
^ | | bit operator |
<= < > >= | Compare operator |
<> == != | is equal to operator |
= %= /= //= -= += *= **= | Assignment Operator |
is is not | identity operator |
in not in | member operator |
and or not | logical operator |
The following example demonstrates the operation of all Python operator precedence:
Instance(Python 3.0+)
The output of the above example:
(a + b) * c / d The result of the operation is: 90.0 ((a + b) * c) / d The result of the operation is: 90.0 (a + b) * (c / d) The result of the operation is: 90.0 The result of a + (b * c) / d is: 50.0