Python Data Types

In programming, a data type defines the kind of data a variable can store, how it is represented in memory, and the operations that can be performed on it.

Python is dynamically typed, which means you don’t need to declare a variable’s data type explicitly; it is automatically inferred based on the value assigned to the variable.

Built-in Data Types in Python

Python has several built-in data types. Here’s a table of all the built-in data types:

CategoryData TypeExampleDescription
Numericint5, -9Integer numbers
float3.14, 0.01Floating-point numbers
complex1+2j, 2-3jComplex numbers
Sequencestr"hello"String(text)
list[1, 2, 3], [a, b, c]Ordered, mutable sequence
tuple(1, 2, 3), (a, b, c)Ordered, mutable sequence
Mappingdict{"a": 1, "b": 2}Key-value pairs
Setset{1, 2, 3}Unordered, unique elements
frozensetfrozenset([1, 2, 3])Immutable set
BooleanboolTrue, FalseLogical values
Binarybytesb"hello", bytes(5)Immutable sequence of bytes
bytearraybytearray(b"hello")Mutable sequence of bytes
memoryviewmemoryview(b"hello")Memory view of binary data
None TypeNoneTypeNoneRepresents the absence of value

In Python, everything is an object, and all data types are instances (objects) of predefined classes. For example, integers are instances of int class, strings belong to the str class, and so on.

We will explore each data type in detail in its own chapter.

Check the Data Type

You can get the data type of any object using the type() function.

For example:

x = 6
print(type(x)) # Output: <class 'int'>