getcontext().prec = 2000
e = Decimal(0)
i = 0
while True:
    fact = math.factorial(i)
e += Decimal(1) / fact
i += 1
if fact > 10 ** 2000:
    break
Calculating e (base of the natural log) to high precision in Python?