Seven operators based on python and seven operators of python
Not much nonsense. The previous section describes data types. This article describes data operations.
In Formula 1 + 2, "1" and "2" are called operands, and "+" are called operators.
Python supports the following operators:
- Arithmetic Operators
- Comparison (relational) Operator
- Value assignment operator
- Logical operators
- Bitwise operators
- Member Operators
- Identity Operators
0x00. Arithmetic Operator
Assume that variable a is 10 and variable B is 20:
Operator |
Description |
Instance |
+ |
Add-add two objects |
The output result of a + B is 30. |
- |
Minus-negative number or subtraction of two objects |
The output result of a-B is-10. |
* |
Multiply by two numbers or return a string that has been repeated for several times. |
A * B Outputs 200 |
/ |
Division-division of two numbers |
The output result of B/a is 2. |
% |
Modulo-returns the remainder after division. |
The output result of B % a is 0. |
** |
Power-returns the y Power of x. |
A ** B is the 10th power of 20 |
// |
Returns the integer portion of the operator. |
9 // 2 the output result is 4, 9.0 // 2.0 the result is 4.0 |
Operator |
Description |
Instance |
+ |
Add-add two objects |
A + B output result 30 |
- |
Minus-get a negative number or a number minus another number |
A-B output result-10 |
* |
Multiply by two numbers or return a string that has been repeated for several times. |
A * B output 200 |
/ |
Divide By-x by y |
B/a output result 2 |
% |
Modulo-return the remainder of the Division |
B % a output result 0 |
** |
Power-returns the y Power of x. |
A ** B is the 10th power, and the output result is 100000000000000000000. |
// |
Returns the integer portion of the operator. |
9 // 2 output result 4, 9.0 // 2.0 output result 4.0 |
0x01.Comparison Operators
Assume that variable a is 10 and variable B is 20:
Operator |
Description |
Instance |
= |
Equal to-compare whether the object is equal |
(A = B) returns False. |
! = |
Not equal to-compare whether two objects are not equal |
(! = B) returns True. |
<> |
Not equal to-compare whether two objects are not equal |
(A <> B) returns True. Similar! = |
> |
Division-two numbers division 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 to indicate true, and 0 to indicate false. This is equivalent to the special variables True and False. Note: These variable names are capitalized. |
(A <B) returns True. |
> = |
-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 |
= |
Equal to-compare whether the object is equal |
(A = B) returns False. |
! = |
Not equal to-compare whether two objects are not equal |
(! = B) returns True. |
<> |
Not equal to-compare whether two objects are not equal |
(A <> B) returns True. This operator is similar! =. |
> |
Greater than-returns whether x is greater than y |
(A> B) return False. |
< |
Less than-returns whether x is less than y. All comparison operators return 1 to indicate true, and 0 to indicate false. This is equivalent to the special variables True and False. Note: These variable names are capitalized. |
(A <B) returns True. |
> = |
-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. |
0x02. Value assignmentOperator
Assume that variable a is 10 and variable B is 20:
Operator |
Description |
Instance |
= |
Simple value assignment operator |
C = a + B: Assign the result of a + B to c. |
+ = |
Addition assignment operator |
C + = a is equivalent to c = c + |
-= |
Subtraction assignment operator |
C-= a is equivalent to c = c- |
* = |
Multiplication and assignment operators |
C * = a is equivalent to c = c * |
/= |
Division assignment operator |
C/= a is equivalent to c = c/ |
** = |
Power assignment operator |
C ** = a is equivalent to c = c ** |
% = |
Modulo assignment operator |
C % = a is equivalent to c = c % |
// = |
Integer Division assignment operator |
C // = a is equivalent to c = c // |
Operator |
Description |
Instance |
= |
Simple value assignment operator |
C = a + B: Assign the operation result of a + B to c. |
+ = |
Addition assignment operator |
C + = a is equivalent to c = c + |
-= |
Subtraction assignment operator |
C-= a is equivalent to c = c- |
* = |
Multiplication and assignment operators |
C * = a is equivalent to c = c * |
/= |
Division assignment operator |
C/= a is equivalent to c = c/ |
% = |
Modulo assignment operator |
C % = a is equivalent to c = c % |
** = |
Power assignment operator |
C ** = a is equivalent to c = c ** |
// = |
Integer Division assignment operator |
C // = a is equivalent to c = c // |
0x03. LogicOperator
Assume that variable a is 10 and variable B is 20:
Operator |
Logical expression |
Description |
Instance |
And |
X and y |
Boolean "and"-If x is False, x and y returns False, otherwise it returns the calculated value of y. |
(A and B) returns 20 |
Or |
X or y |
Boolean "or"-If x is not 0, it returns the value of x; otherwise, it returns the calculated value of y. |
(A or B) returns 10 |
Not |
Not x |
Boolean "not"-If x is True, False is returned. Returns True if x is False. |
Not (a and B) returns False |
Operator |
Logical expression |
Description |
Instance |
And |
X and y |
Boolean "and"-If x is False, x and y returns False, otherwise it returns the calculated value of y. |
(A and B) returns 20. |
Or |
X or y |
Boolean "or"-If x is not 0, it returns the value of x; otherwise, it returns the calculated value of y. |
(A or B) returns 10. |
Not |
Not x |
Boolean "not"-If x is True, False is returned. Returns True if x is False. |
Not (a and B) returns False |
0x04.Bitwise operators
Assume that variable a is 60 and variable B is 13:
Bitwise operators regard numbers as binary values for calculation. The bitwise Algorithm in Python is as follows:
Operator |
Description |
Instance |
& |
Bitwise AND operator |
(A & B) output result 12, binary explanation: 0000 1100 |
| |
Bitwise OR operator |
(A | B) output result 61, binary explanation: 0011 1101 |
^ |
Bitwise OR operator |
(A ^ B) output result 49, binary explanation: 0011 0001 |
~ |
Bitwise Inverse Operator |
(~ A) output result-61, binary explanation: 1100 0011, in the form of a signed binary number Complement |
< |
Left moving Operator |
A <2 output result 240, binary explanation: 1111 0000 |
> |
Right moving Operator |
A> 2 output result 15. Binary explanation: 0000 1111 |
Operator |
Description |
Instance |
& |
Bitwise AND operator |
(A & B) output result 12, binary explanation: 0000 1100 |
| |
Bitwise OR operator |
(A | B) output result 61, binary explanation: 0011 1101 |
^ |
Bitwise OR operator |
(A ^ B) output result 49, binary explanation: 0011 0001 |
~ |
Bitwise Inverse Operator |
(~ A) output result-61, binary explanation: 1100 0011, in a signed binary complement form. |
< |
Left moving Operator |
A <2 output result 240, binary explanation: 1111 0000 |
> |
Right moving Operator |
A> 2 output result 15. Binary explanation: 0000 1111 |
0x05.Member Operators
Operator |
Description |
Instance |
In |
If a value is found in the specified sequence, True is returned; otherwise, False is returned. |
X is in the y sequence. If x is in the y sequence, True is returned. |
Not in |
If no value is found in the specified sequence, True is returned; otherwise, False is returned. |
X is not in the y sequence. If x is not in the y sequence, True is returned. |
0x06. IdentityOperator
Identity operators are used to compare the storage units of two objects.
Operator |
Description |
Instance |
Is |
Is used to determine whether two identifiers are referenced from an object |
X is y. If id (x) is equal to id (y ),IsReturn result 1 |
Is not |
Is not indicates whether two identifiers are referenced from different objects. |
X is not y, if id (x) is not equal to id (y ).Is notReturn result 1 |
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 |
To be continued, next article: Conditional loop statements based on python