Unsupported operand type(s) for +: 'NoneType' and 'int'

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
10 min

banner

# Table of Contents

  1. Unsupported operand type(s) for +: 'NoneType' and 'int'
  2. Unsupported operand type(s) for *: 'NoneType' and 'NoneType'
  3. Unsupported operand type(s) for /: 'NoneType' and 'float'
  4. Unsupported operand type(s) for +: 'NoneType' and 'str'

# Unsupported operand type(s) for +: 'NoneType' and 'int'

The Python "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" occurs when we try to use the addition (+) operator with a None value.

To solve the error, make sure you aren't trying to use the addition (+) operator with a None value and an integer.

typeerror unsupported operand type for plus nonetype and int

Here is an example of how the error occurs.

main.py
num_1 = None num_2 = 15 # โ›”๏ธ TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' result = num_1 + num_2

The variable on the left-hand side of the addition operator stores a None value.

Trying to use the addition (+) operator with a None value and an integer causes the error.

# Checking if the variable isn't None before using (+) operator

Use an if statement if you need to check if a variable doesn't store a None value before using the addition (+) operator.

main.py
num_1 = None num_2 = 15 if num_1 is not None: result = num_1 + num_2 print(result) else: # ๐Ÿ‘‡๏ธ This runs print('variable stores a None value')

checking if variable is not none before using addition operator

The variable stores a None value, so the else block runs.

Alternatively, you can provide a default value if the variable stores None.

main.py
num_1 = None num_2 = 15 if num_1 is None: num_1 = 0 result = num_1 + num_2 print(result) # ๐Ÿ‘‰๏ธ 15

provide default value if variable stores none

We check if the num_1 variable stores a None value and if it does, we set it to 0.

You can set the variable to any other integer that suits your use case.

# Track down where the variable got set to None

To solve the error, you have to figure out where the variable got assigned a None value and correct the assignment.

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.

# Functions that don't return anything return None

Make sure you aren't calling a function that doesn't return anything and expecting the return value to be a number.

main.py
# ๐Ÿ‘‡๏ธ This function returns None def get_num(): print(35) num_2 = 15 # โ›”๏ธ TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' result = get_num() + num_2

functions that return none

The get_num function doesn't return anything, therefore it implicitly returns None.

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

main.py
def get_num(): return 35 num_2 = 15 result = get_num() + num_2 print(result) # ๐Ÿ‘‰๏ธ 50

make sure to return a value from the function

Now the function returns an integer, so using the addition (+) operator works as expected.

# Many built-in methods return None in Python

Note that there are many built-in functions (e.g. sort(), append(), insert()) that mutate the original object in place and return None.

Make sure you aren't storing the result of calling one in a variable.

# Returning a value from a function 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_num(a): if a > 15: return a num_1 = get_num(10) print(num_1) # ๐Ÿ‘‰๏ธ None num_2 = 35 # โ›”๏ธ TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' result = num_1 + num_2

The if block in the get_num function is only run if the supplied number is greater than 15.

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_num(a): if a > 15: return a return 0 # ๐Ÿ‘ˆ๏ธ Return fallback if condition not met num_1 = get_num(10) print(num_1) # ๐Ÿ‘‰๏ธ 0 num_2 = 35 result = num_1 + num_2 print(result) # ๐Ÿ‘‰๏ธ 35

Now the function is guaranteed to return a value regardless of whether the condition is met.

# Table of Contents

  1. Unsupported operand type(s) for *: 'NoneType' and 'NoneType'
  2. Unsupported operand type(s) for /: 'NoneType' and 'float'
  3. Unsupported operand type(s) for +: 'NoneType' and 'str'

# Unsupported operand type(s) for *: 'NoneType' and 'NoneType'

The Python "TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'" occurs when we try to use the multiplication operator with None values.

To solve the error, figure out where the variables got assigned a None value and correct the assignment.

typeerror unsupported operand type for nonetype and nonetype

Here is an example of how the error occurs.

main.py
value_1 = None value_2 = None # โ›”๏ธ TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' result = value_1 * value_2

The variables we are trying to multiply store a None value which caused the error.

We can't use the multiplication operator with a None value on either side.

# Figure out where the variables got set to None

To solve the error, you have to figure out where the variables got assigned a None value and correct the assignment.

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.

# Functions that don't return anything return None

Make sure you aren't calling a function that doesn't return anything and expecting the return value to be a number.

main.py
# ๐Ÿ‘‡๏ธ this function returns None def get_num(): print(10) # โ›”๏ธ TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' result = get_num() * get_num()

The get_num function doesn't return anything, therefore it implicitly returns None.

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

main.py
def get_num(): return 10 result = get_num() * get_num() print(result) # ๐Ÿ‘‰๏ธ 100

The function now returns an integer, so using the multiplication * operator doesn't cause the error.

# Checking if a variable doesn't store a None value

Use an if statement if you need to check whether a variable doesn't store a None value before using the multiplication (*) operator.

main.py
num_1 = None num_2 = 15 if num_1 is not None: result = num_1 * num_2 print(result) else: # ๐Ÿ‘‡๏ธ this runs print('variable stores a None value')

The else block runs because the variable from the example stores a None value.

Alternatively, you can provide a default value if the variable stores None.

main.py
num_1 = None num_2 = 15 if num_1 is None: num_1 = 1 result = num_1 * num_2 print(result) # ๐Ÿ‘‰๏ธ 15

We check if the num_1 variable stores a None value and if it does, we set it to 1.

# Some built-in methods return None

Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.

Make sure you aren't storing the result of calling one in a variable.

# A function only returning a value 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_num(a): if a > 15: return a num_1 = get_num(10) print(num_1) # ๐Ÿ‘‰๏ธ None num_2 = get_num(14) print(num_2) # ๐Ÿ‘‰๏ธ None # โ›”๏ธ TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' result = num_1 * num_2

The if block in the get_num function is only run if the passed-in number is greater than 15.

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_num(a): if a > 15: return a return 2 # ๐Ÿ‘ˆ๏ธ Return fallback if condition not met num_1 = get_num(10) num_2 = get_num(14) result = num_1 * num_2 print(result) # ๐Ÿ‘‰๏ธ 4

Now the function is guaranteed to return a value regardless of whether the condition is met.

# Table of Contents

  1. Unsupported operand type(s) for /: 'NoneType' and 'float'
  2. Unsupported operand type(s) for +: 'NoneType' and 'str'

# Unsupported operand type(s) for /: 'NoneType' and 'float'

The Python "TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'" occurs when we use the division / operator with a None value.

To solve the error, figure out where the variable got assigned a None value and correct the assignment.

typeerror unsupported operand type for slash nonetype and float

Here is an example of how the error occurs.

main.py
num_1 = None num_2 = 2.0 # โ›”๏ธ TypeError: unsupported operand type(s) for /: 'NoneType' and 'float' result = num_1 / num_2

The variable on the left-hand side of the division operator stores a None value.

Trying to divide None by a floating-point number causes the error.

# Track down where the variable got set to None

To solve the error, you have to figure out where the variable got assigned a None value and correct the assignment.

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.

# Functions that don't return anything return None

Make sure you aren't calling a function that doesn't return anything and expecting the return value to be a number.

main.py
# ๐Ÿ‘‡๏ธ This function returns None def get_num(): print(10) num_2 = 2.0 # โ›”๏ธ TypeError: unsupported operand type(s) for /: 'NoneType' and 'float' result = get_num() / num_2

The get_num function doesn't return anything, therefore it implicitly returns None.

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

main.py
def get_num(): return 10 num_2 = 2.0 result = get_num() / num_2 print(result) # ๐Ÿ‘‰๏ธ 5.0

The function now returns a number, so using the division / operator works as expected.

# Check if a variable doesn't store None before dividing

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

main.py
num_1 = None num_2 = 2.0 if num_1 is not None: result = num_1 / num_2 print(result) else: # ๐Ÿ‘‡๏ธ this runs print('variable stores a None value')

The variable stores a None value, so the else block runs.

Alternatively, you can provide a default value if the variable stores None.

main.py
num_1 = None num_2 = 2.0 if num_1 is None: num_1 = 10 result = num_1 / num_2 print(result) # ๐Ÿ‘‰๏ธ 5.0

We check if the num_1 variable stores a None value and if it does, we set it to 10.

Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.

Make sure you aren't storing the result of calling one in a variable.

# Returning a value from a function 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_num(a): if a > 15: return a num_1 = get_num(10) print(num_1) # ๐Ÿ‘‰๏ธ None num_2 = 2.0 # โ›”๏ธ TypeError: unsupported operand type(s) for /: 'NoneType' and 'float' result = num_1 / num_2

The if block in the get_num function is only run if the supplied number is greater than 15.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error in this scenario, 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_num(a): if a > 15: return a return 10 # ๐Ÿ‘ˆ๏ธ Return fallback if condition not met num_1 = get_num(10) print(num_1) # ๐Ÿ‘‰๏ธ 10 num_2 = 2.0 result = num_1 / num_2 print(result) # ๐Ÿ‘‰๏ธ 5.0

Now the function is guaranteed to return a value regardless of whether the condition is met.

# Unsupported operand type(s) for +: 'NoneType' and 'str'

The Python "TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'" occurs when we try to use the addition (+) operator with a None value.

To solve the error, make sure you aren't trying to use the addition (+) operator with a None value and a string.

typeerror unsupported operand type for plus nonetype and str

Here is an example of how the error occurs.

main.py
str_1 = None str_2 = 'world' # โ›”๏ธ TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' result = str_1 + str_2

The variable on the left-hand side of the addition operator stores a None.

Trying to concatenate a None value with a string causes the error.

# Common sources of None values in Python

To solve the error, you have to figure out where the variable got assigned a None value and correct the assignment.

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.

# A function that doesn't return anything returns None

Make sure you aren't calling a function that doesn't return anything and expecting the return value to be a string.

main.py
# ๐Ÿ‘‡๏ธ This function returns None def get_str(): print('hello ') str_2 = 'world' # โ›”๏ธ TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' result = get_str() + str_2

The get_str function doesn't return anything, therefore it implicitly returns None.

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

main.py
def get_str(): return 'hello ' str_2 = 'world' result = get_str() + str_2 print(result) # ๐Ÿ‘‰๏ธ "hello world"

The function now returns a string, so we can use the addition (+) operator to concatenate the strings.

# The print() function returns None

Note that the print() function returns None.

main.py
# โ›”๏ธ TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' print('hello') + 'world'

The print() function returns None, so the code sample tries to use the addition operator with a None value and a string.

Instead, move the string between the parentheses.

main.py
print('hello' + ' world') # ๐Ÿ‘‰๏ธ 'hello world'

Alternatively, you can use a formatted string literal.

main.py
var1 = 'hello' var2 = 'world' result = f'{var1} {var2}!!' print(result) # ๐Ÿ‘‰๏ธ hello world!!

# Checking if the variable is not None before concatenating

Use an if statement if you need to check whether a variable doesn't store a None value before using the addition (+) operator.

main.py
str_1 = None str_2 = 'world' if str_1 is not None: result = str_1 + str_2 print(result) else: # ๐Ÿ‘‡๏ธ this runs print('variable stores a None value')

The str_1 variable stores a None value, so the else block runs.

Alternatively, you can provide a default value if the variable stores None.

main.py
str_1 = None str_2 = 'world' if str_1 is None: str_1 = '' result = str_1 + str_2 print(result) # ๐Ÿ‘‰๏ธ 'world'

We check if the str_1 variable stores a None value and if it does, we set it to an empty string.

# 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_str(a): if len(a) > 5: return a str_1 = get_str('hello') print(str_1) # ๐Ÿ‘‰๏ธ None str_2 = 'world' # โ›”๏ธ TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' result = str_1 + str_2

The if block in the get_str function is only run if the supplied string has a length greater than 5.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error in this scenario, 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_str(a): if len(a) > 5: return a return '' # ๐Ÿ‘ˆ๏ธ Return an empty string if condition not met str_1 = get_str('hello') print(str_1) # ๐Ÿ‘‰๏ธ "" str_2 = 'world' result = str_1 + str_2 print(result) # ๐Ÿ‘‰๏ธ 'world'

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