>>> math.prod([3, 4, 5]) 60 >>> math.prod() 1
Added in 3.9, math.lcm, returns the least common multiple of its integer arguments. As with math.prod, an empty list of arguments returns 1. Extended in 3.9, the related function math.gcd now accepts an arbitrary list of arguments.
These useful additions and extensions are all in the standard math module (along with several others not mentioned here). Previously you’d have to roll your own or rely on e.g. numpy.
I also wanted to mention a nice extension to the power function; in this case to the builtin pow — math.pow remains as it was. Builtin pow takes an optional third parameter
pow(base, exp[, mod]). If
mod is specified it must be a non-zero integer, and both
exp must also be integers. The significant but subtle change made at 3.8 is that
exp can now be a negative integer, enabling modular inverse calculations.
Quoting the documentation:
… If mod is present and exp is negative, base must be relatively prime to mod. In that case, pow(inv_base, -exp, mod) is returned, where inv_base is an inverse to base modulo mod.
Here’s an example of computing an inverse for 38 modulo 97:
>>> pow(38, -1, mod=97) 23 >>> 23 * 38 % 97 == 1 True