Home > Blockchain >  math.prod() is not working in google colab notebook
math.prod() is not working in google colab notebook

Time:10-27

I have executed this code in Gooogle ColabNotebook...As per the link given below... prod() is available under math module. But why prod() is not working..its giving me the error? https://www.w3schools.com/python/ref_math_prod.asp

import math 
print(math.prod([1,2,3,4,5,6,7]))

Output AttributeError: module 'math' has no attribute 'prod'

CodePudding user response:

math.prod() is a new function available in Python versions 3.8 and later. Google Colab's kernel, as of this writing, runs Python 3.6.9. As such, math.prod() won't be available for your use.

You can try to install a Python 3.8 kernel in Colab, but it seems some folks have some mixed results, and the method in the accepted answer is a bit hacky, but might work for your purposes.

CodePudding user response:

Google Colab uses Python 3.6. 9 as of 2021. and math.prod() is in python 3.8 version

you can use this

from functools import reduce
import operator

print(reduce(operator.mul, [1,2,3,4,5,6,7], 1))
  • Related