ValueError and TypeError have very subtle differences
float('Dog') # Gives you an ValueError
The above statement gives you a ValueError, it may sound a little confusing, but now you realize that it makes sense.
The float function can accept a number or a string whose content is numeric as an argument.
Pay attention that I said that it accepts the string if it is a number, otherwise if the argument has a string of characters like dog, the type of argument that the float function receives is correct because it still received a string but the value it received is an inappropriate value for the float function.
According to the above, if an operation or function is given an argument whose type is correct, but the value inside that argument is inappropriate, TypeError will occur.The following example shows that an argument whose type is wrong causes a TypeError
var_ = [1, 2, 3] # Create a iterable
float(var_)
Note that the float function can take a string or a number, but the value inside the variable is an iterable, which does not match the type that the float function takes, so a TypeError is received due to the inappropriate type.
Python documentation about the float function
Python documentation about TypeError
Python documentation about ValueError
123
.'string'
) that's problematic because it is not convertible to a float.float("infinity")
.float("nan")