Basic Math Operations

From Master of Neuroscience Wiki
Revision as of 10:34, 15 October 2025 by Davrot (talk | contribs) (Created page with "Questions to [mailto:davrot@uni-bremen.de David Rotermund] == Number is Python == In Python everything is a class. Numbers are classes as well. === Integer === <syntaxhighlight lang="python">a_number = 0​ print(type(a_number))​ # -> <class 'int'>​</syntaxhighlight> == FLOATing point number == <syntaxhighlight lang="python">a_second_number = 3.33​ print(type(a_second_number))​ # -> <class 'float'>​</syntaxhighlight> == Conversion (i.e. casts) between types...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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
float(x=0.0)
bin(x)
bool(x=False)
bytes(source=b’’)
complex(real=0, imag=0)
hex(x)
int(x=0)
str(object=’’)

Basic math operations​

Order of operations​: 1. Parentheses 2. Exponents 3. Multiplication/Division 4. 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

Inplace Operation

Inplace Operation Short for
a += b a = a + b
a &= b a = a & b
a //= b a = a // b
a <<= b a = a << b
a %= b a = a % b
a *= b a = a * b
a @= b a = a @ b
= b b
a **= b a = a ** b
a >>= b a = a >> b
a -= b a = a - b
a /= b a = a / b
a ^= b a = a ^ b

Boolean Operations — and, or, not​

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

Comparisons

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

Bitwise Operations on Integer Types​

Operation Result
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

More math operations​ (math lib)

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)
Angular conversion
math.degrees(x)
math.radians(x)
Hyperbolic functions
math.acosh(x)
math.asinh(x)
math.atanh(x)
math.cosh(x)
math.sinh(x)
math.tanh(x)

Behind the curtain

{: .topic-optional} This is an optional topic!

If you do a + b, in reality this is internally replaced by add(a, b).

These are the “Mapping Operators to Functions”:

Operation Syntax Function
Addition a + b add(a, b)
Concatenation seq1 + seq2 concat(seq1, seq2)
Containment Test obj in seq contains(seq, obj)
Division a / b truediv(a, b)
Division a // b floordiv(a, b)
Bitwise And a & b and_(a, b)
Bitwise Exclusive Or a ^ b xor(a, b)
Bitwise Inversion ~ a invert(a)
Bitwise Or b or_(a, b)
Exponentiation a ** b pow(a, b)
Identity a is b is_(a, b)
Identity a is not b is_not(a, b)
Indexed Assignment obj[k] = v setitem(obj, k, v)
Indexed Deletion del obj[k] delitem(obj, k)
Indexing obj[k] getitem(obj, k)
Left Shift a << b lshift(a, b)
Modulo a % b mod(a, b)
Multiplication a * b mul(a, b)
Matrix Multiplication a @ b matmul(a, b)
Negation (Arithmetic) - a neg(a)
Negation (Logical) not a not_(a)
Positive + a pos(a)
Right Shift a >> b rshift(a, b)
Slice Assignment seq[i:j] = values setitem(seq, slice(i, j), values)
Slice Deletion del seq[i:j] delitem(seq, slice(i, j))
Slicing seq[i:j] getitem(seq, slice(i, j))
String Formatting s % obj mod(s, obj)
Subtraction a - b sub(a, b)
Truth Test obj truth(obj)
Ordering a < b lt(a, b)
Ordering a <= b le(a, b)
Equality a == b eq(a, b)
Difference a != b ne(a, b)
Ordering a >= b ge(a, b)
Ordering a > b gt(a, b)

In-place Operators

a += b a = iadd(a, b)
a &= b a = iand(a, b)
a += b for a and b sequences a = iconcat(a, b)
a //= b a = ifloordiv(a, b)
a <<= b a = ilshift(a, b)
a %= b a = imod(a, b)
a *= b a = imul(a, b)
a @= b a = imatmul(a, b)
= b a = ior(a, b)
a **= b a = ipow(a, b)
a >>= b a = irshift(a, b)
a -= b a = isub(a, b)
a /= b a = itruediv(a, b)
a ^= b a = ixor(a, b)