If you try to call a float as if it were a function, you will raise the error “TypeError: ‘float’ object is not callable”.
To solve this error, ensure you use operators between terms in mathematical operations and that you do not name any variables “float.
This tutorial will go through how to solve this error with the help of code examples.
Table of contents
TypeError: ‘float’ object is not callable
What is a TypeError?
TypeError occurs in Python when you perform an illegal operation for a specific data type.
What Does Callable Mean?
Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name()
. Let’s look at an example of a working function that returns a string.
# Declare function def simple_function(): print("Hello World!") # Call function simple_function()
Hello World!
We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().
If we try to call a floating-point number, we will raise the TypeError:
number = 5.6 number()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) 1 number = 5.6 2 ----≻ 3 number() TypeError: 'float' object is not callable
Example #1
Let’s look at an example to prove the sum of squares formula for two values. We define two variables with floating-point values, calculate the left-hand side and right-hand side of the formula, and then print if they are equal.
a = 3.0 b = 4.0 lhs = a ** 2 + b ** 2 rhs = (a + b)(a + b) - 2*a*b print(lhs == rhs)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) 2 b = 4.0 3 lhs = a ** 2 + b ** 2 ----≻ 4 rhs = (a + b)(a + b) - 2*a*b 5 print(lhs == rhs) TypeError: 'float' object is not callable
The error occurs because we do not have the multiplication operator * between the two (a + b) terms. The Python interpreter sees this as a call to (a + b) with parameters (a + b).
Solution
We need to put a multiplication operator between the two (a + b) terms to solve the error. Let’s look at the revised code:
a = 3.0 b = 4.0 lhs = a ** 2 + b ** 2 rhs = (a + b)*(a + b) - 2*a*b print(lhs == rhs)
Let’s run the code to see what happens:
True
We get a True
statement, proving that the sum of squares formula works.
Example #2
Let’s look at an example of converting a weight value in kilograms to pounds. We give the conversion value the name “float
” and then take the input from the user, convert it to a floating-point number then multiply it by the conversion value.
float = 2.205 weight = float(input("Enter weight in kilograms: ")) weight_in_lbs = weight * float print(f'{weight} kg is equivalent to {round(weight_in_lbs, 1)} lbs')
Let’s run the code to see what happens:
Enter weight in kilograms: 85 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) 1 float = 2.205 2 ----≻ 3 weight = float(input("Enter weight in kilograms: ")) 4 5 weight_in_lbs = weight * float TypeError: 'float' object is not callable
The error occurs because we assigned the value 2.205 to “float
“. Then we tried to call the built-in float()
method, but float
is now a floating-point number.
Solution
We can name our conversion variable something more meaningful to solve this error. Let’s call it “conversion”. Then we can call the float()
method safely. Let’s look at the revised code:
conversion = 2.205 weight = float(input("Enter weight in kilograms: ")) weight_in_lbs = weight * conversion print(f'{weight} kg is equivalent to {round(weight_in_lbs,1)} lbs')
Let’s run the code to get the result:
Enter weight in kilograms: 85 85.0 kg is equivalent to 187.4 lbs
The program takes the input from the user in kilograms, multiplies it by the conversion value and returns the converted value to the console.
Summary
Congratulations on reading to the end of this tutorial! To summarize, TypeError ‘float’ object is not callable occurs when you try to call a float as if it were a function. To solve this error, ensure any mathematical operations you use have all operators in place. If you multiply values, there needs to be a multiplication operator between the terms. Ensure that you name your float objects after their purpose in the program and not as “float”.
For further reading on “not callable” errors, go to the article: How to Solve Python TypeError: ‘dict’ object is not callable
To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.