Number
There are three numeric types in Python:
- int
- float
- complex
x = 1 # int
y = 2.8 # float
y1 = 35e3 # float
y2 = 12E3 # float
z = 1j # complex
print(type(z)) # complex
# convert from one type to another
a = float(x)
b = int(y)
c = complex(x)
random
python has a built-in random module
import random
print(random.randrange(1,10));
operator
| 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 |
| == | Equal | x == y |
| != | Not equal | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
| and | Returns True if both statements are true | x < 5 and x < 10 |
| or | Returns True if one of the statements is true | x < 5 or x < 4 |
| not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
| is | Returns True if both variables are the same object | x is y |
| is not | Returns True if both variables are not the same object | x is not y |
| in | 如果在y中返回true | x in y |
| not in | 如果不在y中返回true | x not in y |
| &(AND) | Sets each bit to 1 if both bits are 1 | x & y |
| |(OR) | Sets each bit to 1 if one of two bits is 1 | x | y |
| ^(XOR) | Sets each bit to 1 if only one of two bits is 1 | x ^ y |
| ~ (NOT) | Inverts all the bits | ~x |
| << | 左移 | x << 2 |
| >> | 右移 | x >> 2 |
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| //= | x //= 3 | x = x // 3 |
| **= | x **= 3 | x = x ** 3 |
| &= | x &= 3 | x = x & 3 |
| | = | x | = 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 |
| >>= | x >>= 3 | x = x >> 3 |
| <<= | x <<= 3 | x = x << 3 |