Python Datatypes
Datatypes
Based on the nature of data, we can divide the datatypes in Python into many categories. The following table provides the datatypes for each category.
Category | Datatypes |
---|---|
Numeric | int, float, complex |
Boolean | bool |
Sequence | list, tuple, range |
Text | str |
Mapping | dict |
Set | frozenset, set |
Binary | bytes, bytearray, memoryview |
None | NoneType |
Examples
The following table gives Python examples, and the respective constructor function, for each of the datatypes.
The constructor function is used to explicitly specify the datatype when creating the value of that datatype.
Datatype | Example | Builtin Function |
---|---|---|
int | x = 14 | int() |
float | x = 3.14 | float() |
complext | x = 3 + 4j | complex() |
str | x = "Hello World" | str() |
list | x = ["apple", "banana", "cherry"] | list() |
tuple | x = ("apple", "banana", "cherry") | tuple() |
range | x = range(20) | range() |
dict | x = {"name" : "apple", "quantity" : 20} | dict() |
set | x = {"apple", "banana", "cherry"} | set() |
frozenset | x = frozenset({"apple", "banana", "cherry"}) | frozenset() |
bool | x = True | bool() |
bytes | x = b"apple" | bytes() |
bytearray | x = bytearray(5) | bytearray() |
memoryview | x = memoryview(bytes(5)) | memoryview() |
NoneType | x = None | - |