01 / 20
!

Understanding
TypeErrors

Learn why Python raises TypeError, what causes it, and how to diagnose and fix the most common type mismatches in your code.

Python

What Is a TypeError?

A TypeError is a runtime error that Python raises when you try to perform an operation on a value whose data type does not support that operation. It is Python's way of telling you: "these types don't work together."

Python's Type System

Every value in Python has a type. Python is dynamically typed: variables don't declare a type, but the values they hold always have one. Operations are only valid for compatible types.

int
Whole numbers: 42, -7, 0
str
Text strings: "hello", "42"
float
Decimals: 3.14, -0.5
list
Ordered collections: [1, 2, 3]

Why TypeErrors Matter

TypeErrors are one of the most common errors beginners encounter. Understanding them helps you write robust code and debug faster. They reveal misunderstandings about how data flows through your program.

1
User input arrives as a string, but your math expects a number.
2
Function return values may be None when you expect a list or number.
3
Mixing types in expressions (like adding a string to an integer) causes silent logic errors in other languages, but Python catches them.

The Core Rule

Operation (+) int: 5 int: 3 Result 8 Compatible types: ✓

When types are compatible, the operation succeeds. When they're incompatible, Python raises a TypeError.

Anatomy of the Error Message

TypeError: unsupported operand type(s) for +: 'int' and 'str'
1
Error name: "TypeError" tells you it's a type mismatch.
2
Operation: "unsupported operand type(s) for +" tells you which operator failed.
3
Types involved: "'int' and 'str'" tells you exactly which types conflicted.

TypeError vs ValueError

These two errors are often confused. The key difference: TypeError means the type itself is wrong, while ValueError means the type is correct but the actual value is inappropriate.

TypeError ValueError
Cause Wrong type for the operation Right type, wrong value
Example len(42) int("hello")
Fix Convert or change the type Validate the value first
Analogy Putting diesel in a gas car Putting expired gas in a gas car

The 5 Most Common Causes

1
Mixing str and int/float in arithmetic: "5" + 3
2
Calling a non-callable object: x = 5; x()
3
Wrong argument count to a function: len(1, 2)
4
Using None where a value is expected: None + 1
5
Subscripting non-subscriptable types: 42[0]

How to Fix a TypeError

1
Read the error message: identify the operation and the conflicting types.
2
Find the line: Python tells you the exact line number in the traceback.
3
Check actual types: add type(variable) print statements to confirm what you have.
4
Convert explicitly: use int(), str(), or float() to match the expected type.
5
Trace the source: if a variable is None or unexpected, trace back to where it was assigned.

Example 1: String + Integer

age = input("Enter your age: ") next_year = age + 1 # TypeError!
TypeError: can only concatenate str (not "int") to str
age = int(input("Enter your age: ")) next_year = age + 1 # ✓ Works!
✓ Fix: wrap input() with int() to convert the string to an integer.

Example 2: Non-Callable Object

result = 42 print(result()) # TypeError!
TypeError: 'int' object is not callable
# Fix: remove the parentheses result = 42 print(result) # ✓ Prints 42

Example 3: The None Trap

numbers = [3, 1, 4, 1, 5] sorted_nums = numbers.sort() print(sorted_nums[0]) # TypeError!
TypeError: 'NoneType' object is not subscriptable
# Fix option A: use sorted() which returns a new list sorted_nums = sorted(numbers) # Fix option B: sort in place, then use the original numbers.sort() print(numbers[0]) # ✓ Prints 1

Type Conversion Flowchart

Got a TypeError? Check: what types are involved? str + int/float? Use int(), float(), str() NoneType error? Check return values: .sort(), .append() return None Not callable? Remove extra (). Check variable shadows. ✓ TypeError resolved

Wrong Argument Count

def greet(name, greeting): return greeting + ", " + name greet("Alice") # TypeError!
TypeError: greet() missing 1 required positional argument: 'greeting'
def greet(name, greeting="Hello"): return greeting + ", " + name greet("Alice") # ✓ "Hello, Alice"

Common Mistakes

✕ Shadowing built-ins
Naming a variable list, str, or int overwrites the built-in, causing "not callable" later.
✕ Forgetting input() returns str
The most common beginner mistake: doing math on input() without converting to int() or float().
✕ Ignoring None returns
Methods like .sort(), .append(), and .reverse() modify in place and return None.

TypeError Cheat Sheet

Error Message Cause Fix
unsupported operand type(s) Mixing str with int/float int(), float(), str()
'X' object is not callable Extra () on a non-function Remove ()
missing N required argument(s) Too few arguments Add args / defaults
'NoneType' is not subscriptable Using None as a list Check return value
'X' is not iterable Looping over a non-sequence Verify type first

Question 1 of 5

What will the following code produce?
print("Score: " + 95)

Question 2 of 5

A student writes: x = [3,1,2].sort() then tries x[0]. What happens?

Question 3 of 5

Which of the following is the best way to fix this TypeError?
age = input("Age: "); born = 2024 - age

Question 4 of 5

You write list = [1, 2, 3] and later call list("hello"). What error do you get?

Question 5 of 5

What is the key difference between a TypeError and a ValueError?