4 February 2016

Python pow() function returns float or integer?

Today, someone posted this code and asked why x is float ? 
>>> from math import pow
>>> x = pow(5, 2)
>>> x
25.0
It's float because the pow function of math package returns float. But there is a built-in pow function in Python which will return integer in this case. You need to restart the python interpreter before you run the following code : 
>>> a = 5
>>> x = pow(a, 2)
>>> x
25
>>> type(x)
<type 'int'>
So, don't get confused. 

0 comments:

Post a Comment

Thank you for comment. We will try to enhance the quality of this website.