Numbers Flashcards
Infinity & negative infinity
x = float(‘inf’)
print(x) # inf
print(x > 10 ** 1000) # True
y = float(‘-inf’)
print(y) # -inf
print(y < -100000000) # True
Converting floats into fractions
x = 0.25
print(x.as_integer_ratio()) # (1, 4)
Rounding
1234.57
x = 1234.5678
print(round(x, 2))
x = 1234.5678
print(round(x, -2))
.divmod
quotient, remainder = divmod(57, 10)
print(quotient) # 5 (same as 57 // 10)
print(remainder) # 7 (same as 57 % 10)
Floating point inaccuracy
x = 5.000000000000000001
y = 5
print(x == 5) # True
Scientific notation
1500000.0
x = 1.5e6
print(x)
y = 6.4e-5
print(y)
NaN is a float
x = float(‘nan’)
print(x) # nan
Change base
x = int(‘10’, 2) # 2
x = int(‘11’, 2) # 3
x = int(‘100’, 2) # 4
x = int(‘101’, 2) # 5
x = int(‘110’, 2) # 6
x = int(‘111’, 2) # 7
x = int(‘1000’, 2) # 8
x = int(‘100’, 2) # 4
x = int(‘100’, 3) # 9
x = int(‘100’, 4) # 16
x = int(‘100’, 5) # 25