type()
type(x) tells you which class the value x belongs to, such as int, str or ValueError.
Used in 55 lessons of the course, from 00.02.
type(value) returns the class of a value. For 42 it gives <class 'int'>; for "hi", <class 'str'>.
The class has a __name__ attribute holding its name as a string: type(42).__name__ is "int". That is the short text you want to show a person.
The course uses it inside an f-string in lesson 00.02: {type(exc).__name__}. Here exc is a caught exception, and type(exc).__name__ gives the name of its class, such as FileNotFoundError. So the message says what kind of failure happened, not only that something failed.
print(type(3.5).__name__)
To check a type in an if, prefer isinstance(): it also accepts subclasses. Use type() to inspect and display.
Reach for it when: you want to see or display which class a value has, especially the name of an exception in a message.
Examples
Common mistake
value = "5"
if type(value) == "str":
print("es texto")
else:
print("otro tipo")Why: `type(value)` is a class, not a string, so it never equals `"str"`.
How to write it: Compare with the class (type(value) is str) or, better, use isinstance(value, str). To compare names, use type(value).__name__ == "str".
