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. Happy coding! :)

Comments

Unknown said…
>>> from math import pow
>>> x = pow(5,2)
>>> x
25.0
>>> a = 5
>>> x = pow(a,2)
>>> x
25.0
>>> type(x)

This is my code but why returns float??
Tamim Shahriar said…
It returns float because you are using the pow function from math package.
Oshinmonika said…
He is confused Because In your code you also used the 'pow' function but your return type is integer!

>>> a = 5
>>> x = pow(a, 2)
>>> x
25
>>> type(x)
Tamim Shahriar said…
OK, added this line in my post : "You need to restart the python interpreter before you run the following code : "

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code