Python · Stage 1 · Storing Information · Lesson 12 of 50

Checking a Value’s Type

Ask Python what kind of value is stored by using type().

Python’s type function reports a value’s type:

print(type(42))
Line-by-line explanation
  1. 42 is an integer.
  2. type(42) asks for its type.
  3. print displays the answer.
  4. The output contains int.

You can check a stored value too:

price = 4.5
print(type(price))
Line-by-line explanation
  1. The first line stores the float 4.5.
  2. type(price) checks the stored value.
  3. print displays an answer containing float.

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
  1. "25" has quote marks.
  2. type checks its category.
  3. print displays the result.
Hint
  1. Quotes create text.
  2. The Python name for string is str.
  3. Predict before running.
Show Solution
print(type("25"))
Line-by-line explanation
  1. The code was already complete.
  2. Because the value has quotes, its type is str.
  3. The output is <class 'str'>.