Python’s type function reports a value’s type:
print(type(42))Line-by-line explanation
42is an integer.type(42)asks for its type.printdisplays the answer.- The output contains
int.
You can check a stored value too:
price = 4.5
print(type(price))Line-by-line explanation
- The first line stores the float
4.5. type(price)checks the stored value.printdisplays an answer containingfloat.
The answer looks like <class 'float'>. Focus on float; “class” is a later topic.
Tiny Practice: Predict and Check
Predict the type of "25", then run the code.
Starter template
print(type("25"))Line-by-line explanation
"25"has quote marks.typechecks its category.printdisplays the result.
Hint
- Quotes create text.
- The Python name for string is
str. - Predict before running.
Show Solution
print(type("25"))Line-by-line explanation
- The code was already complete.
- Because the value has quotes, its type is
str. - The output is
<class 'str'>.