TypePython Error

TypeError

TypeError: unsupported operand type(s)

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    result = "age: " + 25
TypeError: unsupported operand type(s)

What causes this error

An operation was applied to objects of incompatible types. For example, trying to add a string to an integer, calling something that is not a function, or indexing an object that does not support subscripting.

How to fix it

Check the types of the variables involved using `type()` or `isinstance()`. Convert types explicitly where needed (e.g., `str()`, `int()`, `float()`). Use type hints and a type checker like mypy to catch issues early.

Code that causes this error

Broken
result = "age: " + 25

Fixed code

Fixed
result = "age: " + str(25)

About TypeError

A TypeError is raised when an operation or function receives an argument of an inappropriate type. Python is dynamically typed, so type mismatches are only discovered at runtime. This exception covers a wide range of situations: performing arithmetic between incompatible types (like adding a string and an integer), calling a non-callable object, passing the wrong number of arguments to a function, attempting to subscript an object that does not support indexing, and iterating over a non-iterable.

TypeErrors are among the most common runtime errors in Python, and their messages have become increasingly descriptive in recent Python versions. Reading the full error message carefully usually reveals exactly which types were involved and what operation failed. Type hints and tools like mypy can catch many TypeError-prone patterns before runtime.

Common scenarios

1

Performing operations between incompatible types like strings and integers

2

Calling methods that return None and chaining operations on the result

3

Passing the wrong number or type of arguments to a function

4

Using bracket notation on objects that do not support indexing

Related errors