Last updated: Apr 8, 2024
Reading timeยท6 min
The Python "TypeError: '<' not supported between instances of 'NoneType' and
'int'" occurs when we use a comparison operator between a None
value and an
int
.
To solve the error, figure out where the None
value comes from and correct
the assignment.
Here is an example of how the error occurs.
int_1 = None int_2 = 10 # โ๏ธ TypeError: '<' not supported between instances of 'NoneType' and 'int' if int_1 < int_2: print('success')
We used a comparison operator between values of incompatible types (None
and
int) which caused the error.
None
before the comparisonUse an if
statement if you need to
check whether a variable doesn't store a None value
before using a comparison operator.
int_1 = None int_2 = 10 if int_1 is not None: print(int_1 < int_2) else: # ๐๏ธ This runs print('The variable stores a None value')
The if
statement checks if the variable doesn't store a None
value.
The comparison operator is only used if the variable is not None
, so the error
isn't raised.
Alternatively, you can set the variable to 0
if it stores None
.
int_1 = None int_2 = 10 if int_1 is None: int_1 = 0 print(int_1 < int_2) # ๐๏ธ True
If the int_1
variable stores a None
value, we set it to 0
before using a
comparison operator.
To solve the error, you have to figure out where the None
value comes from and
correct the assignment or conditionally check if the variable doesn't store
None
.
The most common sources of None
values are:
None
implicitly).None
.Make sure you haven't mistakenly reassigned the variable to None
.
int_1 = 50 int_1 = None # โ๏ธ TypeError: '>' not supported between instances of 'NoneType' and 'int' print(int_1 > 25)
We initially set the variable to an integer, but later reassigned it to None
.
Trying to compare a None
value to an integer causes the error.
Functions that don't explicitly return a value return None
.
# ๐๏ธ This function returns None def get_int(): print(20) int_2 = 10 # โ๏ธ TypeError: '>' not supported between instances of 'NoneType' and 'int' if get_int() > int_2: print('success')
You can use a return statement to return a value from a function.
def get_int(): return 20 int_2 = 10 if get_int() > int_2: # โ This runs print('success')
The function now returns an integer, so we can safely compare the two integer values.
Many built-in methods mutate an object in place and
return None, e.g. sort()
, append()
, extend()
,
etc.
Another common source of None
values is trying to
assign the output of print()
to a variable.
result = print(5 + 5) print(result) # ๐๏ธ None
The print()
function prints a message and returns None
.
You can remove the call to print()
and assign the result of the expression to
a variable.
result = 5 + 5 print(result) # ๐๏ธ 10
Another common cause of the error is having a function that returns a value only if a condition is met.
def get_int(a): if a > 5: return a int_1 = get_int(4) print(int_1) # ๐๏ธ None int_2 = 10 # โ๏ธ TypeError: '>' not supported between instances of 'NoneType' and 'int' print(int_1 > int_2)
The if
statement in the get_int
function is only run if the supplied number
is greater than 5
.
None
.To solve the error, you either have to check if the function didn't return
None
or return a default value if the condition isn't met.
def get_int(a): if a > 5: return a return 0 int_1 = get_int(4) print(int_1) # ๐๏ธ 0 int_2 = 10 print(int_1 > int_2) # ๐๏ธ False
Now the function is guaranteed to return a value regardless if the condition is met.
The Python "TypeError: '>' not supported between instances of 'NoneType' and
'float'" occurs when we use a comparison operator between a None
value and a
float
.
To solve the error, track down where the None
value comes from and correct
the assignment.
Here is an example of how the error occurs.
float_1 = None float_2 = 3.14 # โ๏ธ TypeError: '>' not supported between instances of 'NoneType' and 'float' if float_1 > float_2: print('success')
We used a comparison operator between values of incompatible types (None
and
float
) which caused the error.
Use an if
statement if you need to check whether a variable doesn't store a
None
value before using a comparison operator.
float_1 = None float_2 = 3.14 if float_1 is not None: print(float_1 < float_2) else: # ๐๏ธ this runs print('variable stores a None value')
The variable in the example stores a None
value, so the comparison is never
evaluated and the else
block runs.
Alternatively, you can set the variable to 0
if it stores None
.
float_1 = None float_2 = 3.14 if float_1 is None: float_1 = 0 print(float_1 < float_2) # ๐๏ธ True
If the float_1
variable stores a None
value, we set it to 0
before using a
comparison operator.
To solve the error, you have to figure out where the None
value comes from and
correct the assignment or conditionally check if the variable doesn't store
None
.
The most common sources of None
values are:
None
implicitly).None
.Make sure you haven't set a variable that initially was set to a float to a
None
value.
float_1 = 5.5 float_1 = None # โ๏ธ TypeError: '>' not supported between instances of 'NoneType' and 'float' print(float_1 > 3.14)
We initialized the float_1
variable to a floating-point number but it later
got assigned a None
value.
Functions that don't explicitly return a value return None
.
# ๐๏ธ This function returns None def get_float(): print(5.5) float_2 = 3.14 # โ๏ธ TypeError: '>' not supported between instances of 'NoneType' and 'float' if get_float() > float_2: print('success')
You can use a return statement to return a value from a function.
def get_float(): return 5.5 float_2 = 3.14 if get_float() > float_2: # โ this runs print('success')
The function returns a floating-point number, so the comparison works as expected.
Another common cause of the error is having a function that returns a value only if a condition is met.
def get_float(a): if a > 5: return a float_1 = get_float(4) print(float_1) # ๐๏ธ None float_2 = 3.14 # โ๏ธ TypeError: '<' not supported between instances of 'NoneType' and 'float' print(float_1 < float_2)
The if
statement in the get_float
function is only run if the passed-in
number is greater than 5
.
None
.To solve the error, you either have to check if the function didn't return
None
, or return a default value if the condition is not met.
def get_float(a): if a > 5: return a return 0 # ๐๏ธ return 0 if condition not met float_1 = get_float(4) print(float_1) # ๐๏ธ 0 float_2 = 3.14 print(float_1 < float_2) # ๐๏ธ True
Now the function is guaranteed to return a value regardless of whether the condition is met.