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 | – |
Frequently Asked Questions
Answer:
int, float, and complex are the numeric datatypes in Python.
Answer:
True and False are the two possible values of boolean type in Python.
Answer:
You can use 'str' to define a string of characters in Python. The str class also has built-in methods to work on the string values, which makes it the trivial choice for the character strings.
Answer:
frozenset and set in Python are used to store a set of items. The basic difference between the two datatypes is that: frozenset cannot be modified, whereas set can be modified.