Quick reference to the math functions and methods of Python.
Function | Does what |
---|---|
complex(x) | Converts x to a complex number with real part x and imaginary part zero. |
complex(x, y) | Converts x and y to a complex number with real part x and imaginary part y. Both x and y are numeric expressions. |
degrees(x) | Converts angle x from radians to degrees. |
float(x) | Converts x to a floating-point number. |
radians(x) | Converts angle x from degrees to radians. |
int(x) | Converts x to an integer. |
long(x) | Converts x to a long integer. |
Function | Does what |
---|---|
abs(x) | The absolute value of x: the (positive) distance between x and zero. |
ceil(x) | The ceiling of x: the smallest integer not less than x |
cmp(x, y) | -1 if x < y, 0 if x == y, or 1 if x > y |
e | The constant e |
exp(x) | The exponential of x (e |
fabs(x) | The absolute value of x. |
floor(x) | The floor of x: the largest integer not greater than x. |
log(x) | The natural logarithm of x, for x>0. |
log10(x) | The base-10 logarithm of x, for x>0. |
max(x1, x2,...) | The largest of the arguments: the value closest to positive infinity. |
min(x1, x2,...) | The smallest of the arguments: the value closest to negative infinity. |
modf(x) | 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. |
pi | The constant Pi (π) |
pow(x, y) | The value of x**y. |
round(x [,n]) | Rounds x to n digits from the decimal point. Python rounds away from zero: round(0.5) is 1.0 and round(-0.5) is -1.0. |
sqrt(x) | The square root of x, for x>0. |
Operator | Does what |
---|---|
+ | Addition Adds values on either side of the operator. |
- | Subtraction Subtracts right hand operand from left hand operand. |
* | Multiplication Multiplies values on either side of the operator |
/ | Division Divides left hand operand by right hand operand |
% | Modulus Divides left hand operand by right hand operand and returns remainder |
** | Exponent Performs exponential (power) calculation on operators |
// | Floor Division A division where the digits after the decimal point are removed from the result. But if one of the operands is negative, the result is floored, i.e. rounded away from zero (towards negative infinity) |
Function | Does what |
---|---|
choice(src) | A random item from a list, tuple, or string. |
randrange ([start,] stop [,step]) | A randomly selected element from range(start, stop, step) |
random() | A random float r, such that 0 is less than or equal to r and r is less than 1 |
seed([x]) | Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None. |
shuffle(lst) | Randomizes the items of a list in place. Returns None. |
uniform(x, y) | A random float r, such that x is less than or equal to r and r is less than y |
Function | Does what |
---|---|
acos(x) | Returns the arc cosine of x, in radians. |
asin(x) | Returns the arc sine of x, in radians. |
atan(x) | Returns the arc tangent of x, in radians. |
atan2(y, x) | Returns atan(y / x), in radians. |
cos(x) | Returns the cosine of x, in radians. |
hypot(x, y) | Returns the Euclidean norm (hypotenuse), sqrt(x*x + y*y). |
sin(x) | Returns the sine of x, in radians. |
tan(x) | Returns the tangent of x radians. |
More: