A Python variable can refer to values of different types, and you may need to check one before using it. A data type identifies the class of a value and the operations that class supports.

I’ll explain what Python data types describe, then show how to check a value’s exact class or whether it belongs to a class.

TL;DR: Python data types describe values

A Python data type is the class of a value, and type() shows that class while isinstance() checks whether a value belongs to a class or its subclass.

  • Numbers use int, float or complex.
  • Text uses str, while True and False use bool.
  • Lists and dictionaries can change in place. Tuples and strings cannot.
  • A variable name can later refer to a value of a different type.

What is a data type in Python?

A data type identifies an object’s class and the operations it supports. Python determines a value’s type when the object is created, so you can use a variable name without a type declaration.

A variable name refers to an object. Reassignment makes the name refer to a different object, while the original object keeps its type.

Category Built-in types What they represent
Numeric int, float, complex Whole, floating-point and complex numbers
Text str Unicode text
Sequence list, tuple, range Ordered values accessed by position
Mapping dict Key-to-value associations
Set set, frozenset Unique, hashable elements
Boolean bool True or False
Null value NoneType The single value None
Binary bytes, bytearray, memoryview Byte data and views into a buffer

The categories tell you how a value behaves. A list and a tuple can hold mixed values, but only the list lets you replace an item in place. A class can define a custom type when built-ins do not fit a program’s data.

How to check a Python value’s data type step by step

Run a short script with representative values, then read the class name or test for an accepted class. I ran the source file below with the displayed command and captured the complete output without an interactive prompt.

Step 1: Put example values in a source file

Save this as python_data_types_demo.py and run python3 python_data_types_demo.py. The script prints each value’s type and then checks the list and number with isinstance().

values = {
    "count": 7,
    "price": 2.5,
    "label": "blue",
    "enabled": True,
    "missing": None,
    "items": [1, "two"],
    "pair": (1,),
    "flags": {1, 2},
    "record": {"id": 7},
    "raw": b"ok",
}

for name, value in values.items():
    print(f"{name}: {type(value)}")

print("items is a list:", isinstance(values["items"], list))
print("count is numeric:", isinstance(values["count"], (int, float)))

The captured run printed:

count: 
price: 
label: 
enabled: 
missing: 
items: 
pair: 
flags: 
record: 
raw: 
items is a list: True
count is numeric: True
The script prints each sample value’s type and checks two values with isinstance().

The output shows type() returning a class such as int or NoneType. Its display is normal.

Step 2: Choose an exact or family check

Use type(value) when you need the exact class name for inspection. Use isinstance(value, expected_class) when code should accept a class and its subclasses. The final two output lines show a list check and a check for either int or float.

For example, isinstance(True, int) is also true because bool is a subclass of int. When a branch should accept the Boolean class specifically, check for bool explicitly.

Python data type edge cases to know

Construction syntax and input conversion can change how a value behaves. These cases help show which checks your code needs.

Case What happens What to do
Mixed list A list can contain values of different types, such as the number and text in the demo. Check each item if later operations require one type.
Single-item tuple Parentheses group a value. The trailing comma makes (1,) a tuple. Include the comma when building a one-item tuple.
Immutable value A string or tuple cannot be changed in place, though its variable name can be rebound. Create a new value when its contents must differ.
Empty set Empty braces create a dictionary, not a set. Call set() to create an empty set.
Text from input() input() returns str, including when the characters look numeric. int() conversion can raise ValueError. Convert deliberately and handle invalid text where input is accepted.
Set members and dictionary keys These values must be hashable. Mutable lists cannot serve in these positions. Use an immutable value such as a tuple when its contents can be hashable.

A tuple can contain a list. You cannot replace the tuple’s references, but the inner list can still change. For choosing among Python collections, see data structures in Python.

Type hints also describe expected values without changing ordinary runtime checks. The Python type hints guide explains that separate distinction.

Conclusion: choose types by how data behaves

Choose a collection by how you will use its contents: a list for values that change, a tuple for a fixed sequence, a set for unique membership and a dictionary for key-to-value lookup. The built-in types reference documents the remaining classes and their operations.

FAQ

These answers cover common questions about Python types and values.

How do I check a variable’s data type in Python?

Call type(value) to see its exact class, or isinstance(value, class) to test whether it belongs to a class or subclass.

Can a Python list contain different data types?

Yes. A list can contain values of different types, though the code that processes those values must handle the types it expects.

What does None have as its data type?

The value None has the type NoneType. Check for it with value is None.

Share.
Leave A Reply