跳到主要内容

Number

There are three numeric types in Python:

  1. int
  2. float
  3. 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

operatornameexample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor divisionx // y
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y
andReturns True if both statements are truex < 5 and x < 10
orReturns True if one of the statements is truex < 5 or x < 4
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)
isReturns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectx is not y
in如果在y中返回truex in y
not in如果不在y中返回truex not in y
&(AND)Sets each bit to 1 if both bits are 1x & y
|(OR)Sets each bit to 1 if one of two bits is 1x | y
^(XOR)Sets each bit to 1 if only one of two bits is 1x ^ y
~ (NOT)Inverts all the bits~x
<<左移x << 2
>>右移x >> 2
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
| =x | = 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3
Loading Comments...