Numberstype
int (10,100)
long (5167466L)
float (12.5)
complex (12.8-3.14j)
a = 10
b = 12.5
c = 5147638L
d = 5.1 - 3.14j
print type(a)
print type(b)
print type(c)
print type(d)
结果:
<type 'int'>
<type 'float'>
<type 'long'>
<type 'complex'>
Number TypeConversion
· int(x) to convert x to aplain integer.
· long(x) to convert x toa long integer.
· float(x) to convert x toa floating-point number.
· complex(x)/ complex(x, y) toconvert x to a complex number with real part x and imaginary part zero.
Mathematical Functions
Function |
Returns ( description ) |
The absolute value of x: the (positive) distance between x and zero. |
|
The ceiling of x: the smallest integer not less than x |
|
-1 if x < y, 0 if x == y, or 1 if x > y |
|
The exponential of x: ex |
|
The absolute value of x. |
|
The floor of x: the largest integer not greater than x |
|
The natural logarithm of x, for x> 0 |
|
The base-10 logarithm of x for x> 0 . |
|
The largest of its arguments: the value closest to positive infinity |
|
The smallest of its arguments: the value closest to negative infinity |
|
The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float. |
|
The value of x**y. |
|
x rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0. |
|
The square root of x for x > 0 |
Random Number Functions
随机数字用于游戏,模拟,测试,安全和隐私应用。 Python包含以下常用功能。
Function |
Description |
A random item from a list, tuple, or string. |
|
A randomly selected element from range(start, stop, step) |
|
A random float r, such that 0 is less than or equal to r and r is less than 1 |
|
Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None. |
|
Randomizes the items of a list in place. Returns None. |
|
A random float r, such that x is less than or equal to r and r is less than y |