-
SyntaxError: Messed up the syntax—unbalanced parentheses, inappropriate indentation, etc. -
NameError: Use of an undefined identifier. -
TypeError: Operation involves an object of an inappropriate type. -
ValueError: Operation involves an object of the correct type but with an inappropriate value. -
ZeroDivisionError: Division by 0, floor-division by 0, or remainder mod 0. -
IndexError: String, tuple, or list index out of bounds. -
RecursionError: Maximum recursion depth exceeded—mostly in the case of unbounded recursion.
Difference between Type and Value error
Quick Comparison
| Feature | TypeError | ValueError |
|---|---|---|
| Root cause | An operation or function is applied to an object of an inappropriate type. | An argument has the right type, but an inappropriate value. |
| Typical question | ”Can I do this with a str / int / None?" | "Is this specific str / int / float valid here?” |
| Example | 5 + "10" (int + str) | int("abc") ("abc" is a str, which is the right type for int(), but its value isn’t a valid base-10 integer) |
1. TypeError (Wrong Type)
A TypeError happens when the type of a value doesn’t match what an operator or function expects.
-
Adding incompatible types:
Python
total = 0 total += "10" # TypeError: 'int' and 'str' cannot be added with += -
Passing a non-iterable to a function expecting an iterable:
Python
print(*42) # TypeError: print() argument after * must be an iterable, not int -
Passing the wrong type to
int():Python
int([1, 2, 3]) # TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
2. ValueError (Right Type, Wrong Value)
A ValueError happens when Python accepts the type of the input, but the value itself is invalid, out of range, or doesn’t fit the expected format.
-
Converting an invalid string to an
int:Python
int("abc") # ValueError: invalid literal for int() with base 10: 'abc' int("3.5") # ValueError: invalid literal for int() with base 10: '3.5'(Notice why
int("abc")is aValueErrorrather than aTypeError:int()expects astr, and"abc"is astr—it’s just not a valid integer representation.) -
Math functions on out-of-domain numbers:
Python
import math math.sqrt(-1) # ValueError: math domain error (-1 is a valid number type, but negative) -
Looking up a missing item in a list:
Python
["a", "b"].index("z") # ValueError: 'z' is not in list