Basic math operations
{:.no_toc}
* TOC
{:toc}
Questions to David Rotermund
Number is Python
In Python everything is a class. Numbers are classes as well.
Integer
a_number = 0
print(type(a_number)) # -> <class 'int'>
FLOATing point number
a_second_number = 3.33
print(type(a_second_number)) # -> <class 'float'>
Conversion (i.e. casts) between types
a_second_number = 3.33
a_second_number = int(a_second_number)
print(type(a_second_number)) # -> <class 'int'>
Examples are here:
Order of operations:
- Parentheses
- Exponents
- Multiplication/Division
- Addition/Subtraction
Operator |
Description |
x + y |
sum of x and y |
x - y |
difference of x and y |
x * y |
product of x and y |
x / y |
quotient of x and y |
x // y |
floored quotient of x and y |
x % y |
remainder of x / y |
-x |
x negated |
+x |
x unchanged |
Operator |
Description |
abs(x) |
absolute value or magnitude of x |
int(x) |
x converted to integer |
float(x) |
x converted to floating point |
complex(re, im) |
a complex number with real part re, imaginary part im. im defaults to zero. |
c.conjugate() |
conjugate of the complex number c |
divmod(x, y) |
the pair (x // y, x % y) |
pow(x, y) |
x to the power y |
x ** y |
x to the power y |
"True division"
print(5 / 2) # -> 2.5
print(6 / 2) # -> 3.0
“Floor division”
print(5 // 2) # -> 2
print(6 // 2) # -> 3
Operation |
Result |
x or y |
if x is false, then y, else x |
x and y |
if x is false, then x, else y |
not x |
if x is false, then True, else False |
Operation |
Meaning |
< |
strictly less than |
<= |
less than or equal |
> |
strictly greater than |
>= |
greater than or equal |
== |
equal |
!= |
not equal |
is |
object identity |
is not |
negated object identity |
Operation |
Result |
x | y |
bitwise or of x and y |
x ^ y |
bitwise exclusive or of x and y |
x & y |
bitwise and of x and y |
x << n |
x shifted left by n bits |
x >> n |
x shifted right by n bits |
~x |
the bits of x inverted |
You need to include the math lib for that! (Only once per .py file and in the beginning of the file)
However, don't get used to it. As a data scientist you will not use it. You will use Numpy.
import math
print(math.cos(math.pi))
Examples
Number-theoretic and representation functions |
math.ceil(x) |
math.comb(n, k) |
math.fabs(x) |
math.factorial(n) |
math.floor(x) |
math.fmod(x, y) |
math.fsum(iterable) |
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) |
math.isfinite(x) |
math.isinf(x) |
math.isnan(x) |
math.perm(n, k=None) |
math.prod(iterable, *, start=1) |
math.trunc(x) |
Constants |
math.pi |
math.e |
math.inf |
math.nan |
Power and logarithmic functions |
math.cbrt(x) |
math.exp(x) |
math.exp2(x) |
math.expm1(x) |
math.log(x[,base]) |
math.log1p(x) |
math.log2(x) |
math.log10(x) |
math.pow(x, y) |
math.sqrt(x) |
Trigonometric functions |
math.acos(x) |
math.asin(x) |
math.atan(x) |
math.atan2(y, x) |
math.cos(x) |
math.sin(x) |
math.tan(x) |
math.dist(p, q) |
Hyperbolic functions |
math.acosh(x) |
math.asinh(x) |
math.atanh(x) |
math.cosh(x) |
math.sinh(x) |
math.tanh(x) |