28

I can't completely understand the difference between Type and Value error in Python3x.

Why do we get a ValueError when I try float('string') instead of TypeError? shouldn't this give also a TypeError because I am passing a variable of type 'str' to be converted into float?

In [169]: float('string')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-169-f894e176bff2> in <module>()
----> 1 float('string')

ValueError: could not convert string to float: 'string'
4
  • 12
    Some strings can be converted to float, for example 123.
    – user202729
    Jan 19, 2018 at 14:35
  • 1
    And it's the value of the string ('string') that's problematic because it is not convertible to a float.
    – user202729
    Jan 19, 2018 at 14:38
  • 5
    Another valid example is float("infinity"). Jan 19, 2018 at 14:42
  • 2
    Another one is float("nan") Jan 19, 2018 at 14:51

5 Answers 5

50

A Value error is

Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value

the float function can take a string, ie float('5'), it's just that the value 'string' in float('string') is an inappropriate (non-convertible) string

On the other hand,

Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError

so you would get a TypeError if you tried float(['5']) because a list can never be converted into a float.

Cite

1
  • I was confused when float('string') was raising ValueError instead of TypeError. I totally ignored the fact that float('5') is a valid piece of code in python. Thank you. May 22, 2022 at 6:21
3

ValueError a function is called on a value of the correct type, but with an inappropriate value

TypeError : a function is called on a value of an inappropriate type

3

They are well explained in Python Documentation.

I would add examples for both of them:

TypeError:

10 + 'a'

ValueError:

int("hello")
0

Just would like to add one more point to David's answer.

TypeError can also occur, when we pass incorrect no of arguments to a function.

eg:

def hello(int x,int y):
    pass

hello(12)

This also gives you TypeError:

hello() missing 1 required positional argument: 'y'

1
0

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.