3.1. Numerics & Arithmetics 🧐 - JulTob/Python GitHub Wiki

Numerics and Arithmetics 🧐

x + y
x - y
x * y
x / y
x // y  #  rounded division
x % y  #  rest
-x
+x
divd, res = divmod(x,y)  # ( x // y , x % y )
abs(x)
pow(x,y)  # x to the y-th power
x ** y

int(x)
float(x)
complex(re, im)
c.conjugate()
round(x, precision)

bin(x) # To binary

Integers

one = 1
two = 2 
some_number = -10000
Casting = int( ‘6’)
zero_to_zero = 0 ** 0 # = 1

Floats

menu_price = 15.80
casted_int2float = float(4)
casted_string = float("4")
Not_A_Number = NaN
infinite = inf
neg_infinite = -inf
f = 2.5
b = f.is_integer()
Scientific = 2.3e5

Complex

Vect = 15 + 2j

Bitwise

Or_Byte = Byte_1 | Byte_2
Xor_Byte = Byte_1 ^ Byte_2
And_Byte = Byte_1 & Byte_2
Shifted_L_Byte = Byte_1 << n
Shifted_R_Byte = Byte_1 >> n
Inverted_Byte = -Byte_1

Operations

+    -    *    /    % (modulus)   ** (power)
=+    =-    =*    =/    =% (modulus)   

Division legacy

Python 3:
7/2 # = 3.5

Python 2:
7/2 # = 3
7./2 # = 3.5  float(7)/2

Growth

final = initial * (g_rate ** time)