First, arithmetic operations
| operation (character) |
Description |
Example |
| + |
Two objects added |
2 + 3 results for 5 |
| - |
Subtraction of two objects |
3-2 Results of 1 |
| * |
Two numbers multiply or return a sequence that repeats several times |
2 * 3 results for 6; ' ABC ' * 2 results for ' ABCABC ' |
| / |
Divide two numbers |
3/2 Results of 1.5 |
| // |
The integer portion of the returned quotient. |
3//2 result is 1, 3//2.0 result is 1.0 |
| % |
Rest/modulo, return the remainder of division |
3% 2 results are 1, 3% 2.0 result is 1.0 |
| ** |
exponentiation/Sub-square |
2 * * 3 result is 8 |
Second, assignment operation
| operation (character) |
Description |
Example |
| = |
Simple assignment operators |
A = 5, B = 3, c = A-a |
| += |
Addition assignment operator |
A + = b equals A = a + b |
| -= |
Subtraction assignment operator |
A-= B is equivalent to A = a A |
| *= |
Multiplication assignment operator |
A = b equals A = a b |
| /= |
Division assignment operator |
A/= b equals a = A/b |
| //= |
Take the divisible assignment operator |
A//= b equals a = A//b |
| %= |
Modulo assignment operator |
A%= b equals a = a% b |
| **= |
Power assignment operator |
A = b equals A = a b |
Third, comparative operations
Python has 8 comparison operations, and they have the same precedence. The comparison operation can be arbitrarily connected, such as x< y <= z equals x < y and y <= Z, but only the first form of y is evaluated once. Also, when x < Y is not true, neither of these forms of Z will be evaluated.
| operation (character) |
Description |
Example |
| < |
Strictly less than |
3 < 5 results for true,5 < 5 result is false |
| <= |
Less than or equal to |
3 <= 5 result for true,5 <= 5 result is true |
| > |
Strictly greater than |
5 > 3 results for true,5 > 5 result is False |
| >= |
Greater than or equal to |
5 >= 3 result for true,5 >= 5 result is true |
| == |
Equals |
1 = = 1.0 = True result is true |
| != |
Not equal to |
|
| Is |
Determines whether two identifiers are referenced from an object |
X is y, if ID (x) = = ID (y), that is, X also y points to the same memory address, the result is 1, otherwise the result is 0 |
| is not |
Determine if two identifiers are referenced from different objects |
X is not y, if ID (x)! = ID (y), that is, X and Y point to different memory addresses, the result is 1, otherwise the result is 0 |
Description
A) different types of objects are compared and never equal (except for different numeric types);
b) When the <, <=, >, and >= operators are thrown typeerror exceptions in these cases: (1) for the comparison of complex numbers and other built-in numeric types, (2) to compare objects of different types, when comparisons are not possible, (3) In other cases not defined;
c) Different instances of a class are usually not equal unless the class defines the __eq__ () method;
D) An instance of a class cannot be sorted relative to another instance of the same class or other class unless the class defines enough methods __lt__ (), __le__ (), __gt__ (), __ge__ (). If you want to compare the general meaning of Operators, __lt__ () and __eq__ () are sufficient;
e) The behavior of the IS and is not operators are not customizable, and they can be applied to two different types of objects without exception.
f) The other two operations with the same syntactic precedence are in and not in, which support objects of sequence, collection, and mapping types.
g) The result of the comparison operation is a Boolean value: True or False
Four, logical operation "True" value test (Truth value testing)
Before explaining "Boolean operations," Let's start with a special operation in Python-the "true" value test.
Any object in Python can be tested with a "true" value. The "true" value test is understood here: any object in Python can be converted to a Boolean value, and this "true" value test is the process of getting a Boolean value for an object.
In Python, only the following values correspond to a Boolean value of false:
None
False
Number type 0, for example: 0, 0.0, 0j
Any empty sequence, such as: ', (), []
Any empty mappings, such as: {}
An instance of a user-defined class-a __bool__ () or __len__ () method is defined in the user-defined class, and an integer 0 or boolean value is returned when the instance calls the method false
In addition, all other values correspond to Boolean values that are true, so many types of objects are always true.
The true value test can be used in an if or while condition, or as an operand of a Boolean operation.
Boolean operation (Boolean Operations)
The logical operations in Python are called Boolean operations (Boolean Operations), and the operators include: and (with), or (or), not (non).
The following are described in ascending order of their precedence:
| arithmetic |
Results |
| x or y |
If the truth value of x tests the result to false, the result of the operation is Y, otherwise the value of x |
| x and y |
If the truth value of x tests the result to false, then the result of the operation is the value of x, otherwise the value of y |
| not x |
If the truth value of x evaluates to False, the result of the operation is true, otherwise the result is false |
Description
A) or is a short-circuit operator, which means that the second parameter is evaluated only if the first parameter evaluates to false;
b) and is also a short-circuit operator, which means that the second parameter is evaluated only if the first parameter evaluates to TRUE;
c) The NOT operator has a lower precedence than a non-Boolean operator, so not a = = B is interpreted as not (a = = B), and if written as a = = not B, the packet syntax is incorrect.
Five, bit arithmetic
Bitwise operations are calculations that convert numbers to binary, and the bitwise operators include the following:
Assume:
A = 60, the corresponding binary format is 0011 1100
b = 13, the corresponding binary format is 0000 1101
| operator |
Description |
Example |
| & |
Bitwise AND: Two values that participate in the operation, if the corresponding bits are 1, the bit result is 1, otherwise 0 |
A & B corresponds to a binary result of 0000 1100 and a decimal of 12 |
| Vertical bar |
Bitwise OR: Two values that participate in the operation, as long as the corresponding bits by one is 1 o'clock, the bit results in 1 |
A vertical bar B corresponds to a binary result of 0011 1101 and a decimal of 61 |
| ^ |
Bitwise XOR: Two values participating in the operation, when the corresponding bits is not the same, the result is 1, otherwise the result is 0 |
A ^ b corresponds to a binary result of 0011 0001 with a decimal of 49 |
| ~ |
Bitwise negation: This is the monocular operator, with only one value participating in the operation, the operation is reversed for each bits, i.e. 1 is changed to 0, and 0 is changed to 1 |
The binary result of the ~a is 1100 0011, and the decimal number is-61 |
| << |
Left-shift operator: the operands of each bits all left a number of bits, high drop, low 0, the result is equivalent to the operand multiplied by 2 of the N-square, positive and negative symbols do not change |
The binary result of a << 2 is 1111 0000, and the decimal number is 240 |
| >> |
Right-shift operator: each bits of the operand is shifted to the right by a number of bits, resulting in the equivalent of the operand divided by 2 N, the positive and negative symbols do not change |
The binary result of a >> 2 is 0000 1111, and the decimal is 15 |
Operators in Python