There are many built-in functions available in Python that save you time in developing your application. However, if you do not know how to use them correctly, it will cause unexpected the TypeError: Unsupported operand type for *: builtin_function_or_method and float. To overcome this problem, we have to invoke the function. Check out this article for further information.
Why do we get this error?
This error occurs when we work with a built-in function in Python and a variable of type float but forget the function call. Let’s see an example of how the error occurs:
import math floatNumber = 8.25 result = math.pow * floatNumber
As you can see, we should have invoked the math.pow() method. Hence the TypeError: “unsupported operand type(s) for *: ‘builtin_function_or_method’ and ‘float'” indicate that we are trying to multiply a method by a float number.
Solution for the “Unsupported operand type for *: builtin_function_or_method and float” error
To solve this problem, it’s straightforward, make sure you call the function. For a better visualization, we continue with the error example above:
import math floatNumber = 8.25 result = math.pow(2,3) * floatNumber print("The result is: " + str(result))
The result is: 66.0
Parentheses are used to call the function. At this point, the error is gone. You’ve got the result.
Note that if the method you use requires passing arguments, pass them in.
In this example, we use math.pow() method that requires the user to pass in two parameters, x and y. The return result of the function is the value x to the power of y. The value 66 we get in the output is the result of multiplying 2 power 3 by 8.25
In Python’s math module, there are many other available functions you can refer to, such as:
math.sqrt(), math.fabs(), math.factorial(), … etc.
import math results =  # Returns the square root of 64 firstResult = math.sqrt(64) # Returns the absolute value of -99 secondResult = math.fabs(-99) # Returns the factorial of 8 thirdResult = math.factorial(8) results.append(firstResult) results.append(secondResult) results.append(thirdResult) print("The results are respectively: ") print(results)
The results are respectively: [8.0, 99.0, 40320]
Sometimes it could be helpful to check the type of the returned value before performing other calculations to avoid unexpected errors. The built-in type() class or the instanceof() method can help you with that.
That’s the end of this article. We hope you have understood the information related to the error “Unsupported operand type for *: builtin_function_or_method and float” that we have conveyed through this article. Make sure you call the function to avoid this kind of error.
Maybe you are interested:
- TypeError: unsupported operand type(s) for -: str and int
- TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’
Name of the university: HUST