Python Data Types

Python data types describe what kind of value a variable holds and what you can do with it. You can add two numbers, but you cannot add a number to a list. Knowing the built-in data types helps you pick the right tool and understand error messages.

Built-in Python Data Types

  • Text: str
  • Numbers: int, float, complex
  • Boolean: bool (True or False)
  • Sequences: list, tuple, range
  • Mapping: dict
  • Sets: set, frozenset
  • Binary: bytes, bytearray
  • Nothing: NoneType (the single value None)

Getting the Data Type

The type() function tells you the data type of any value. Below, each variable holds a different type.

values = ["hello", 42, 3.14, True, [1, 2], (1, 2), {"a": 1}, {1, 2}, None]

for v in values:
    print(type(v).__name__, "->", v)
str -> hello
int -> 42
float -> 3.14
bool -> True
list -> [1, 2]
tuple -> (1, 2)
dict -> {'a': 1}
set -> {1, 2}
NoneType -> None

Checking a Type with isinstance()

In real programs you usually want to ask "is this value a number?" rather than print its type. isinstance() returns True or False, and it accepts a tuple of types to check several at once.

price = 9.99

print(isinstance(price, float))
print(isinstance(price, str))
print(isinstance(price, (int, float)))
True
False
True

Mutable vs Immutable Types

Some types can be changed after they are created (mutable) and some cannot (immutable). Lists, dictionaries and sets are mutable. Numbers, strings, tuples and booleans are immutable: any "change" actually creates a new value.

fruits = ["apple", "mango"]
fruits.append("kiwi")      # the same list is changed
print(fruits)

word = "cat"
upper_word = word.upper()  # a new string is created
print(word, upper_word)
['apple', 'mango', 'kiwi']
cat CAT

The None Value

None represents "no value". Functions that do not return anything give back None, and it is common as a default before real data arrives. Compare with None using is, not ==.

result = None

if result is None:
    print("No result yet")
No result yet

Frequently asked questions

How many data types are there in Python?

Python has around 14 built-in types: str, int, float, complex, bool, list, tuple, range, dict, set, frozenset, bytes, bytearray and NoneType. You can also create your own types with classes.

What is the difference between type() and isinstance()?

type() returns the exact type of a value. isinstance() checks whether a value is an instance of a type or any of its subclasses, which makes it the better choice for checks in code.

Is a string mutable in Python?

No. Strings are immutable. Methods like upper() or replace() return a new string and leave the original unchanged.