< Not supported between instances of 'NoneType' and 'int'

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
6 min

banner

# Table of Contents

  1. < not supported between instances of 'NoneType' and 'int'
  2. '>' not supported between instances of 'NoneType' and float

# < not supported between instances of 'NoneType' and 'int'

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.

typeerror not supported between instances of nonetype and int

Here is an example of how the error occurs.

main.py
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.

# Checking if a variable doesn't store None before the comparison

Use an if statement if you need to check whether a variable doesn't store a None value before using a comparison operator.

main.py
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')

check if not none before comparison

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.

main.py
int_1 = None int_2 = 10 if int_1 is None: int_1 = 0 print(int_1 < int_2) # ๐Ÿ‘‰๏ธ True

set variable to zero if it stores none

If the int_1 variable stores a None value, we set it to 0 before using a comparison operator.

# Track down where the None value comes from

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:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

# Make sure you haven't reassigned a variable to None

Make sure you haven't mistakenly reassigned the variable to None.

main.py
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 return anything return None

Functions that don't explicitly return a value return None.

main.py
# ๐Ÿ‘‡๏ธ 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')

function returning none

You can use a return statement to return a value from a function.

main.py
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 return None

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.

main.py
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.

main.py
result = 5 + 5 print(result) # ๐Ÿ‘‰๏ธ 10

# Having a function that returns a value only if a condition is met

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
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)

only returning value if condition is met

The if statement in the get_int function is only run if the supplied number is greater than 5.

In all other cases, the function doesn't return anything and ends up implicitly returning 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.

main.py
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.

# '>' not supported between instances of 'NoneType' and float

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.

typeerror not supported between instances of nonetype and float

Here is an example of how the error occurs.

main.py
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.

# Check if the variable is not None before comparing

Use an if statement if you need to check whether a variable doesn't store a None value before using a comparison operator.

main.py
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.

main.py
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.

# Track down where the variable got assigned a None value

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:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

# Reassigning a float variable by mistake

Make sure you haven't set a variable that initially was set to a float to a None value.

main.py
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.

# Make sure to return from your functions

Functions that don't explicitly return a value return None.

main.py
# ๐Ÿ‘‡๏ธ 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.

main.py
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.

# A function returning a value only if a condition is met

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
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.

In all other cases, the function doesn't return anything and ends up implicitly returning 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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev