TypeError
Python Programming Language
Severity: ModerateWhat Does This Error Mean?
A TypeError means you tried to use a value in a way that does not match its type. For example, trying to add a number and a piece of text together, or calling something as a function when it is not a function. Python is strict about types — you have to convert them explicitly if you want to mix them.
Affected Models
- Python 2.x
- Python 3.x
- All Python versions
Common Causes
- Trying to combine a string and a number with + without converting one of them first
- Calling a variable as if it were a function — for example, writing result() when result is just a number
- Passing the wrong number of arguments to a function (too many or too few)
- Using an operation on the wrong type — like trying to sort a number, or get the length of an integer
- Trying to use None as a value in a calculation (a function returned None instead of a number)
How to Fix It
-
Read the full error message. It usually says something like 'unsupported operand type(s) for +: int and str'. This tells you exactly what types are clashing.
Note which two types are mentioned. That tells you what needs to be converted.
-
If you are combining a number and a string, convert one of them. Use str(number) to turn a number into text, or int(text) to turn text into a number.
Example: 'Hello ' + str(42) works. 'Hello ' + 42 raises a TypeError.
-
If the error says something is 'not callable', you may have overwritten a function name with a variable, or forgotten to define the function.
Example: if you write 'print = 5' anywhere, Python replaces the print function with a number. Then calling print() fails.
-
Check the number of arguments you are passing to a function. If a function expects two arguments and you pass one, you get a TypeError.
Look at how the function is defined (the def line) to see how many parameters it accepts.
-
If a variable might be None, add a check before using it. Use 'if result is not None:' before performing calculations on it.
A function that has no return statement returns None by default. Assign a real value before the function exits.
When to Call a Professional
TypeErrors are always something you can fix yourself. Read the error message carefully — Python usually tells you exactly which types were involved. Use the built-in type() function to check what type a variable is if you are unsure.
Frequently Asked Questions
What is the difference between a TypeError and a ValueError?
A TypeError means the wrong kind of thing was used — like mixing a number and text. A ValueError means the right kind of thing was used but the value itself was bad — like trying to convert the word 'hello' into an integer. TypeError is about the type. ValueError is about the value inside that type.
How do I check what type a variable is?
Use the built-in type() function. For example: print(type(my_variable)) will print something like <class 'int'> or <class 'str'>. This is useful when you are not sure what type a function returned.
Why does Python not just automatically convert types like other languages do?
Python is deliberately strict about this to prevent bugs. Automatic conversion (called implicit coercion) can cause unexpected results. For example, in JavaScript, '5' + 3 gives '53' instead of 8. Python forces you to be explicit, which makes your code more predictable and easier to debug.