【PYTHON】Python的操作子。 Python Operators
Python 的操作子:
- 運算操作子(Arithmetic Operators):
Operator | Name | Example | |
---|---|---|---|
+ | Addition | x + y | |
- | Subtraction | x - y | |
* | Multiplication | x * y | |
/ | Division | x / y | |
% | Modulus | x % y | |
** | Exponentiation | x ** y | |
// | Floor division | x // y |
- 附值操作子(Assignment Operators):
Operator | Example | Same As | Try it |
---|---|---|---|
= | x = 5 | x = 5 | Try it » |
+= | x += 3 | x = x + 3 | Try it » |
-= | x -= 3 | x = x - 3 | Try it » |
*= | x *= 3 | x = x * 3 | Try it » |
/= | x /= 3 | x = x / 3 | Try it » |
%= | x %= 3 | x = x % 3 | Try it » |
//= | x //= 3 | x = x // 3 | Try it » |
**= | x **= 3 | x = x ** 3 | Try it » |
&= | x &= 3 | x = x & 3 | Try it » |
|= | x |= 3 | x = x | 3 | Try it » |
^= | x ^= 3 | x = x ^ 3 | Try it » |
>>= | x >>= 3 | x = x >> 3 | Try it » |
<<= | x <<= 3 | x = x << 3 | Try it » |
- 比較操作子:
Operator | Name | Example | Try it |
---|---|---|---|
== | Equal | x == y | Try it » |
!= | Not equal | x != y | Try it » |
> | Greater than | x > y | Try it » |
< | Less than | x < y | Try it » |
>= | Greater than or equal to | x >= y | Try it » |
<= | Less than or equal to | x <= y | Try it » |
- 邏輯操作子:
Operator | Description | Example | Try it |
---|---|---|---|
and | Returns True if both statements are true | x < 5 and x < 10 | Try it » |
or | Returns True if one of the statements is true | x < 5 or x < 4 | Try it » |
not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) | Try it » |
- 定義操作子:
Operator | Description | Example | Try it |
---|---|---|---|
is | Returns True if both variables are the same object | x is y | Try it » |
is not | Returns True if both variables are not the same object | x is not y | Try it » |
- Membership操作子:
Operator | Description | Example | Try it |
---|---|---|---|
in | Returns True if a sequence with the specified value is present in the object | x in y | Try it » |
not in | Returns True if a sequence with the specified value is not present in the object | x not in y | Try it » |
- 二元操作子:
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
留言
張貼留言