ValueError and TypeError in python


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'
Asked By: thileepan



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.


Answered By: David

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

Answered By: Imad77

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.


def hello(int x,int y):


This also gives you TypeError:

hello() missing 1 required positional argument: ‘y’

Answered By: adi

They are well explained in
Python Documentation.

I would add examples for both of them:


10 + 'a'


Answered By: Yousef Dajani

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 

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

Answered By: Taha jalilian
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.