白皮书下载
Operators and Expressions
Operator is used to perform program code operations and will operate on more than one operand item. The composition of variables and operators is called an expression. The various operators are described in detail below.
Arithmetic operator
The following assumed variables: a = 10, B = 20:
|
operator |
describe |
example |
|---|---|---|
|
+ |
Add - adds two objects |
A + B output result 30 |
|
- |
Minus - get a negative number or subtract one number from another |
A - B output result -10 |
|
* |
Multiply - multiplies two numbers or returns a string that is repeated several times |
A * B output result 200 |
|
/ |
Divide - x divided by Y |
B / A output result 2 |
|
% |
Modulo - returns the remainder of division |
B % A output result 0 |
|
** |
Power - returns the y-power of X |
A * * B is the 20th power of 10, and the output result 100000000000000000000 |
|
// |
Integer division - returns the integer portion of the quotient (rounded down) |
>>> 9//24>>> -9//2-5 |
Comparison operator
The following assumes that variable a is 10 and variable B is 20:
|
operator |
describe |
example |
|---|---|---|
|
== |
Equal - whether the comparison objects are equal |
(a == b) return False. |
|
!= |
Not equal - compares whether two objects are not equal |
(a != b) return true. |
|
<> |
Not equal - compares whether two objects are not equal |
(a <> b) return true。This operator is similar to != . |
|
> |
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 for true and 0 for false. This is equivalent to the special variables true and false, respectively. |
(a < b) return true. |
|
>= |
Greater than or equal - Returns whether x is greater than or equal to y. |
(a >= b) return False. |
|
<= |
Less than or equal to - Returns whether x is less than or equal to y. |
(a <= b) return true. |
Assignment Operators
The following assumes that variable a is 10 and variable B is 20:
|
operator |
describe |
example |
|
= |
Simple assignment operator |
C = A + b assigns the operation result of A + B to C |
|
+= |
Additive 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 |
|
%= |
Modulo assignment operator |
C %= A is equivalent to C = C % A |
|
**= |
Power assignment operator |
C **= A is equivalent to C = C ** A |
|
//= |
Integer division assignment operator |
C //= A is equivalent to C = C // A |
Bitwise Operators
Bitwise operators calculate numbers as binary. In the following table, variables a are 60 and B are 13:
|
operator |
describe |
example |
|
& |
Bitwise and operator: two values participating in the operation. If both corresponding bits are 1, the result of this bit is 1, otherwise it is 0 |
(A & B) output result 12, binary interpretation: 0000 1100 |
|
| |
Bitwise OR operator: as long as one of the corresponding two binary bits is 1, the result bit is 1 |
(A | B) output result 61, binary interpretation: 0011 1101 |
|
^ |
Bitwise exclusive or operator: when two corresponding binary bits are different, the result is 1 |
(A ^ B) output result 49, binary interpretation: 0011 0001 |
|
~ |
Bitwise negation operator: negates each binary bit of data, that is, changes 1 to 0 and 0 to 1~ x be similar to - x-1 |
(~A) output result - 61, binary interpretation: 1100 0011, in the complement form of a signed binary number. |
|
<< |
Move left operator: all binary bits of the operand are shifted to the left by several bits. The number of bits to be moved is specified by the number on the right. The high bit is discarded and the low bit is supplemented by 0 |
A << 2 output result 240, binary interpretation: 1111 0000 |
|
>> |
Move right operator: move all the binary bits of the operand on the left of "> >" to the right several bits, > > The number on the right specifies the number of bits to mo |
A >> 2 output result 15, binary interpretation: 0000 1111 |
Logical operator
The following variables are assumed to be 10 for a and 20 for B:
|
operator |
Logical expression |
describe |
example |
|
and |
x and y |
Boolean and - X and Y return false if x is false, otherwise it returns the calculated value of Y. |
(A and B) return 20 |
|
or |
x or y |
Boolean or - if x is non-zero, it returns the value of X, otherwise it returns the calculated value of Y. |
(A or B) return 10 |
|
not |
not x |
Boolean not - Returns FALSE if x is true. If x is false, it returns true. |
not(A and B) return False |




