Python3 operator and python3 Operator
1. python Arithmetic Operators
>>> A = 15 >>> B = 5 >>> a + B # Add 20 >>> a-B # Minus 10 >>> a * B # multiply 75>> a/B # division, for floating point number 3.0 >>> a % B # modulo, remainder 0 >>> a ** B # power, power B of a: 759375> a // B # returns the integer part 3 of the operator.
2. python comparison Operators
>>> 1 = 1 # equal to True >>> 1! = 1 # not equal to False >>> 1! = 2 True >>> 1> 2 # greater than False >>> 2> 1 True >>> 2 <1 # less than False >>> 1 <2 True >>> 2> = 1 # greater than or equal to True> 2> = 2 True> 2> = 3 False >>> 2 <= 3 # less than or equal to True> 2 <= 2 True> 2 <= 1 False
3. python value assignment operator
>>> A = 123 >>> a = B = c = 123 # multi-variable assignment >>> a, B, c =, 'python' # assign values to multiple variables in sequence >>>, B => c = 0> c + = a # is equivalent to c = c + a >>> c10 >>> c = 30 >> c-= a # is equivalent to c = c-a> c20> c = 5> c * = a # is equivalent to c = c * a> c50>> c = 30 >>> c/= a # is equivalent to c = c/a >>>> c3.0 >>> c = 36 >>> c % = a # is equivalent to c = c % a >>> c6 >>> c = 2 >>> c ** = a # is equivalent to c = c ** a >>> c1024 >>> c = 33 >>> c // = a # is equivalent to c = c // a >>> c3
4. python bitwise operators
Bitwise operators regard numbers as binary values for calculation.
& Bitwise AND operator: the two values involved in the operation. If the corresponding bits are both 1, The result bit is 1. Otherwise, the value is 0.
| Bitwise OR operator: if one of the two binary numbers is 1, The result bit is 1.
^ Bitwise OR operator: When the binary numbers of the two numbers are different, the result is 1.
~ Bitwise inversion OPERATOR: returns the inverse of each binary bit of the data. That is, 1 is changed to 0, and 0 is changed to 1 .~ X is similar to-X-1
<Left moving OPERATOR: each binary of an operation number shifts several places to the left. The number on the right specifies the number of digits to be moved. The value is discarded at a high position and 0 is supplemented at a low position.
> Right-moving OPERATOR: shifts all the binary values of the Left operator to several places to the right. ">" the number on the right specifies the number of digits to move.
A = 60 #60 = 0011 1100b = 13 #13 = 0000 1101c = 0c = a & B #12 = 0000 1100 all are true, that is, 1; otherwise, 0 print ('C's & value: ', c) c = a | B #61 = 0011 1101 if one is true, it is 1; otherwise, it is 0 print ('C | value:', c) c = a ^ B #49 = 0011 0001 when the binary result is in a different phase, it is 1. Otherwise, it is 0 print ('C's ^ value: ', c) c = ~ A #-256-195 = 1100 0011 the value of the inverse direction can be 256-(195)-61 print (~ of 'c ~ A value: ', c) c = a <2 #240 = 1111 0000 2 to the left of a binary position, after moving, use 0 to fill print (<value: ', c) c = a> 2 #15 = 0000 1111 pairs of binary a shifted to the right of 2 places, use 0 to fill with print ('C >>> value: ', c) c & Value: 12c | value: 61c ^ value: 49c ~ Value a:-61c <value: 240c> value: 155. python logical operators
And x and y Boolean 'and', if x is false, x and y returns false, otherwise it returns the calculated value of y
Or x or y Boolean "or"-If x is True, it returns the value of x; otherwise, it returns the calculated value of y.
Not x Boolean "Non"-If x is True, False is returned. Returns True if x is False.
>>> A = 10 >>> B = 20 >>> x = 0 >>> a and B # output the value of B 20 only when both a and B are true>> a and x # a is true, if x is false, the output value is 00 >>> x and a0 >>> a or B # If either a or B is true, the first output value is true. 10 >>> a or x10 >>> y = 0 >>> x or y # when neither of the preceding conditions is met, the output is 00 >>> not a # Take the inverse value of a False >>> not y # y itself is false, and the inverse value is true.
6. python member operators
Python also supports member operators. The test instance contains a series of members, including strings, lists, or tuples.
In returns True if a value is found in the specified sequence. Otherwise, False is returned.
Not in returns True if no value is found in the specified sequence. Otherwise, False is returned.
>>> L1 = [1, 2, 3, 4, 5] >>> tuple = ('A', 'B', 'C') >>> dict = {'k1 ': 'python', 'k2': 'java', 'k3 ': 'php' }>>> 'A' in tuple # The member returns trueTrue in THE tuples >>> 1 in l1True >>> '1' in l1 # note that this is a string, therefore, falseFalse is returned. >>> 6 not in l1 # trueTrue is returned when the member is not identified. >>> 'D' not in tupleTrue >>> 'python' in dict. values () # judge whether 'python' returns trueTrue> 'k1 'not in dict in the dictionary value. values () # k1 is not a member of values, so trueTrue >>> 'k5 'not in dict is returned. keys () True7. python identity Operators
Identity operators are used to compare the storage units of two objects.
Is used to determine whether two identifiers are referenced from one object. If the two identifiers are the same object, true is returned. Otherwise, false is returned.
Is not determines whether two identifiers reference different objects. If not the same object is referenced, true is returned; otherwise, false is returned.
>>> A = 11 >>> B = 'AB' >>> a is B # Return falseFalse by referencing different objects >>> a is not B # Return trueTrue by referencing different objects>> c = 11 >>> a is c # The application returns trueTrue from the same memory address object >>> id () 8956800 >>>> id (c) 8956800 >>> a is not c # returns falseFalse for the same address object
# Is and = # is used to determine whether two variables reference the same object, = is used to determine whether the value of the referenced variables is equal> a = [1, 2, 3, 4] >>> B = a >>> id (a) 139653642018888 >>> id (B) 139653642018888 >>> a is B # returns trueTrue if the memory address is the same >>> a = B # returns trueTrue if the value is the same >>> B = a [:] # assign the value of list a to B. In this case, the memory address of B is changed, but the value is the same as that of a>>> id (B) 139653642018952 >>> B [1, 2, 3, 4] >>> B is a # If the memory address is different, falseFalse is returned >>> B = a # If the value is the same, trueTrue is returned.
8. python operator priority
The following table lists all operators with the highest to lowest priority:
| Operator |
Description |
| ** |
Index (highest priority) |
| ~ +- |
Flip by bit, one-dollar plus sign and minus sign (the last two methods are named + @ and -@) |
| */% // |
Multiplication, division, modulo and Division |
| +- |
Addition and subtraction |
| >>< < |
Right Shift, Left Shift Operator |
| & |
AND' |
| ^ | |
Bitwise operators |
| <=<>>= |
Comparison Operators |
| <>==! = |
Equal to operator |
| = % =/= // =-= + = * = ** = |
Value assignment operator |
| Is not |
Identity Operators |
| In not in |
Member Operators |
| Not or and |
Logical operators |