Article Directory
- Preface
- Python Error TypeError: Int object Is Not Callable
- Add missing operators in python to fix TypeError: 'int' object is not callable in Python
- Change the variable name in Python to fix TypeError: 'int' object is not callable
- Python Error TypeError: Unhashable Type: List
- TypeError: unhashable type: 'list' in Python
- Hash functions in Python
- Fix TypeError: unhashable type: 'list' in Python
- Python Error TypeError: Unsupported Operand Type(s) for +: ‘NoneType’ and ‘Int’
- Error in Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' Cause
- Fix TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
- Summarize
Preface
In this article, let’s take a look at some practical examplesTypeError
Error and give a solution. You can learn from one example and apply it to other aspects in your future coding, so as to effectively avoid this type ofTypeError
mistake. You need to collect this content and read it slowly. It can also be used as a dictionary. If you encounter related ones, you can directly refer to the corresponding solutions.
OK, Let’s go.
Python Error TypeError: Int object Is Not Callable
This is usedPython encodingOne of the common mistakes encountered when encountering. Python can throw this error in a variety of cases.
Mainly when you miss math while performing calculationsOperatorsThis happens when using built-in functions as variables in your code.
This section will discuss all scenarios and fixesTypeError: 'int' object is not callable
solution.
Add missing operators in python to fix TypeError: 'int' object is not callable in Python
Sometimes you may forget to add math operators to your code. As a result, you will get a TypeError: ‘int’ object is not callable.
Let's take this simple Python script as an example.
marks_obtained=450
total_marks=600
percentage=100(marks_obtained/total_marks)
print("The percentage is:", percentage)
- 1
- 2
- 3
- 4
Output:
Traceback (most recent call last):
File "c:\Users\rhntm\", line 3, in <module>
percentage=100(marks_obtained/total_marks)
TypeError: 'int' object is not callable
- 1
- 2
- 3
- 4
It returns an error because the multiplication operator is missing in the percentage calculation code. You can add multiplication operators in your code*
To resolve this issue.
marks_obtained=450
total_marks=600
percentage=100*(marks_obtained/total_marks)
print("The percentage is:", percentage)
- 1
- 2
- 3
- 4
Output:
The percentage is: 75.0
- 1
Change the variable name in Python to fix TypeError: 'int' object is not callable
If you use the built-in function name of the variable and call the function later, you will receive an error message‘int’ object is not callable。
In the following example, we declare a sum variable,sum()
is a built-in function in Python to addIteratoritem.
num=[2,4,6,8]
sum=0
sum=sum(num)
print("The sum is:", sum)
- 1
- 2
- 3
- 4
Output:
Traceback (most recent call last):
File "c:\Users\rhntm\", line 3, in <module>
sum=sum(num)
TypeError: 'int' object is not callable
- 1
- 2
- 3
- 4
Since we used the sum variable and then calledsum()
Functions to calculate the sum of numbers in the list, so it raises a TypeError because the sum variable overridessum()
method.
You can fix this type of error by renaming the variable name. Here we change the variable sum to Total.
num=[2,4,6,8]
total=0
total=sum(num)
print("The sum is:", total)
- 1
- 2
- 3
- 4
Output:
The sum is: 20
- 1
You can see that the code was successfully run this time.
Now you know how to fix the‘int’ object is not callablemistake. We hope these solutions will be helpful to you.
Python Error TypeError: Unhashable Type: List
This section will be discussedTypeError: unhashable type: 'list'
And how to fix it in Python.
TypeError: unhashable type: ‘list’ in Python
Let's first look at how this error occurs.
This error occurs when you pass non-hashable objects (such as lists) as keys to a Python dictionary or find the hash value of a function.
Dictionary is a type of PythonData structure, works as key-value pairs, each key has a corresponding value, to access the value of the value, you will need keys like array index.
Dictionary syntax:
dic ={ "key": "Values"}
- 1
Hashable objects are objects whose values do not change over time but remain unchanged, and tuples and strings are types of hashable objects.
Code:
# creating a dictionary
dic = {
# list as a key --> Error because lists are immutable
["a","b"] : [1,2]
}
print(dic)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Output:
TypeError: unhashable type: 'list'
- 1
We use list["a","b"]
As a key, butCompilerThrow oneTypeError: unhashable type: ‘list’。
Let's manually find the hash value of the list.
Code:
lst = ["a","b"]
hash_value = hash(lst)
print(hash_value)
- 1
- 2
- 3
- 4
- 5
Output:
TypeError: unhashable type: 'list'
- 1
hash()
Functions are used to find the hash value of a given object, but the object must be immutable, such as strings, tuples, etc.
Hash functions in Python
hash()
A function is an encryption technique that encrypts an immutable object and assigns a unique value to it, called the hash value of the object. It provides a unique value of the same size regardless of the data size.
Code:
string_val = "String Value"
tuple_val = (1,2,3,4,5)
msg= """Hey there!
Welcome to """
print("Hash of a string object\t\t", hash(string_val))
print("Hash of a tuple object\t\t", hash(tuple_val))
print("Hash of a string message\t", hash(tuple_val))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Output:
Hash of a string object -74188595
Hash of a tuple object -1883319094
Hash of a string message -1883319094
- 1
- 2
- 3
The hash values are the same size and are unique for each value.
Fix TypeError: unhashable type: 'list' in Python
To fix TypeError in Python, you must use immutable objects as keys for dictionary andhash()
The parameters of the function. Note that in the above code, the hash() function works perfectly with mutable objects such as tuples and strings.
Let's see how to fix theTypeError: unhashable type: ‘list’ 。
Code:
# creating a dictionary
dic = {
# string as key
"a" : [1,2]
}
print(dic)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Output:
{'a': [1, 2]}
- 1
This time we provide a string "a" as the key, and using it is nice because the string is mutable.
Python Error TypeError: Unsupported Operand Type(s) for +: ‘NoneType’ and ‘Int’
In Python, when you add integer values to null values, it appearsTypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
. We will discuss Python errors and how to fix them in this article.
Error in Python TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ Cause
The Python compiler will throwTypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’Because we are operating on two values with different data types. In this case, the data types of these values are int and null, and the error will tell you that the operation is not supported because the operands of the + operator int and null are invalid.
Code example:
a = None
b = 32
print("The data type of a is ", type(a))
print("The data type of b is ", type(b))
# TypeError --> unsupported operand type(s) for +: 'NoneType' and 'int'
result = a+b
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Output:
The data type of a is <class 'NoneType'>
The data type of b is <class 'int'>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
- 1
- 2
- 3
As we can see in the output of the program above, the data type of a is NoneType, and the data type of b is int. When we try to add variables a and b, we encounterTypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’。
There are some similar situations here that can also cause TypeError because their data types are different.
# Adding a string and an integer
a = "string"
b = 32
a+b # --> Error
# Adding a Null value and a string
a = None
b = "string"
a+b # --> Error
# Adding char value and a float
a = 'D'
b = 1.1
a+b # --> Error
Fix the TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' in Python
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
It cannot be arithmetic with null, and the above program has proven that it throws an error. Also, if you have any similar situations, type convert the value before performing any arithmetic operation or other required tasks.
To fix this error, you can perform any arithmetic operations on it with a valid data type, type convert the value, or if your function returns a null value, you can use the try-catch block to avoid program crashes.
Code example:
a = "fql"
b = "jiyik"
print("The data type of a is ", type(a))
print("The data type of b is ", type(b))
result = a+b
print(result)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
Output:
The data type of a is <class 'str'>
The data type of b is <class 'str'>
fqljiyik
- 1
- 2
- 3
As you can see, a and b have similar data types, they are connected perfectly without throwing any errors, because the properties of these two variables are the same.
Fix TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’
Code example:
def sum_ab(a, b=None):
return a+b #TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
sum_ab(3)
- 1
- 2
- 3
- 4
Output:
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
- 1
In the above code, the sum_ab() function has two parameters, a and b, and b is assigned to a null value (the default parameter), and the function returns the sum of a and b.
Suppose you only provide one parameter sum_ab(3). This function will automatically trigger the default parameter to None and cannot be added, as shown in the above example.
In this case, if you are not sure which function raisesTypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’, you can use the try-catch mechanism to overcome such errors.
Code example:
try:
def sum_ab(a, b=None):
return a+b
sum_ab(3)
except TypeError:
print(" unsupported operand type(s) for +: 'int' and 'NoneType' \n The data types are a and b are invalid")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Output:
unsupported operand type(s) for +: 'int' and 'NoneType'
The data types are a and b are invalid
- 1
- 2
try-catch
Blocks can help you explain the error and prevent program crashes.
Summarize
We have introduced the TypeError situation through three examples above. First, how it occurs, and then give the cause and solution. Due to space reasons, we will give these three examples first in this article. We continue in the next post. See you in the next article.