Last updated: Apr 8, 2024
Reading timeยท10 min
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.
Here is an example of how the error occurs.
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.
Use an if
statement if you need to
check if a variable doesn't store a None value
before using the
addition (+) operator.
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 variable stores a None
value, so the else
block runs.
Alternatively, you can provide a default value if the variable stores None
.
num_1 = None num_2 = 15 if num_1 is None: num_1 = 0 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 0
.
You can set the variable to any other integer that suits your use case.
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:
None
implicitly).None
.None
Make sure you aren't calling a function that doesn't return anything and expecting the return value to be a number.
# ๐๏ธ 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
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.
def get_num(): return 35 num_2 = 15 result = get_num() + num_2 print(result) # ๐๏ธ 50
Now the function returns an integer, so using the addition (+) operator works as expected.
None
in PythonNote 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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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_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.
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.
Here is an example of how the error occurs.
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.
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:
None
implicitly).None
.Make sure you aren't calling a function that doesn't return anything and expecting the return value to be a number.
# ๐๏ธ 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.
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.
None
valueUse an if
statement if you need to check whether a variable doesn't store a
None
value before using the multiplication (*) operator.
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
.
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
.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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_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.
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.
Here is an example of how the error occurs.
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.
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:
None
implicitly).None
.Make sure you aren't calling a function that doesn't return anything and expecting the return value to be a number.
# ๐๏ธ 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.
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.
Use an if
statement if you need to check whether a variable doesn't store a
None
value before using the division operator.
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
.
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
.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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.
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.
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.
Here is an example of how the error occurs.
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.
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:
None
implicitly).None
.Make sure you aren't calling a function that doesn't return anything and expecting the return value to be a string.
# ๐๏ธ 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.
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.
Note that the print()
function returns None
.
# โ๏ธ 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.
print('hello' + ' world') # ๐๏ธ 'hello world'
Alternatively, you can use a formatted string literal.
var1 = 'hello' var2 = 'world' result = f'{var1} {var2}!!' print(result) # ๐๏ธ hello world!!
Use an if
statement if you need to check whether a variable doesn't store a
None
value before using the addition (+) operator.
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
.
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
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.
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.