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:
Category | Data Type | Example | Description |
Numeric | int | 5 , -9 | Integer numbers |
float | 3.14 , 0.01 | Floating-point numbers | |
complex | 1+2j , 2-3j | Complex numbers | |
Sequence | str | "hello" | String(text) |
list | [1, 2, 3] , [a, b, c] | Ordered, mutable sequence | |
tuple | (1, 2, 3) , (a, b, c) | Ordered, mutable sequence | |
Mapping | dict | {"a": 1, "b": 2} | Key-value pairs |
Set | set | {1, 2, 3} | Unordered, unique elements |
frozenset | frozenset([1, 2, 3]) | Immutable set | |
Boolean | bool | True , False | Logical values |
Binary | bytes | b"hello" , bytes(5) | Immutable sequence of bytes |
bytearray | bytearray(b"hello") | Mutable sequence of bytes | |
memoryview | memoryview(b"hello") | Memory view of binary data | |
None Type | NoneType | None | Represents 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'>