Python Data Types: Data types are an important concept in any programming language. Because, variables can store different types of data, and different types can do different things. Python has a variety of data types built in by default in these categories.
Text Data Type | str |
Numeric Data types | Int, float, complex |
Sequence Data Types | List, tuple, range |
Mapping Data Type | dict |
Set Data Types | set, frozenset |
Boolean Data Type | bool |
Binary Data Types | bytes, bytearray, memoryview |
None Data Type | NoneType |
How To Get The Python Data Types?
You can get the data type of any object by using the type() function:
Example
Print the data type of the variable x:
x = 5 print(type(x))
Setting The Python Data Types
In Python, the data type is set when you assign a value to a variable.
Example | Data Type |
x = “Hello World” | str |
x = 40 | int |
x = 40.5 | float |
x = 1f | complex |
x = [“apple”, “banana”, “cherry”] | list |
x = (“apple”, “banana”, “cherry”) | tuple |
x = range(6) | range |
x = {“name” : “Venom”, “age” : 40} | dict |
x = {“apple”, “banana”, “cherry”} | set |
x = frozenset({“apple”, “banana”, “cherry”}) | frozenset |
x = True | bool |
x = b”Hello” | bytes |
x = bytearray(10) | bytearray |
x = memoryview(bytes(10)) | memoryview |
x = None | NoneType |
Setting The Specific Data Type
If you want to specify the data type, you can use the following constructor functions.
Example | Data Type |
x = str(“Hello World”) | str |
x = int(20) | int |
x = float(20.5) | float |
x = complex(1j) | complex |
x = list([“apple”, “banana”, “cherry”]) | list |
x = tuple((“apple”, “banana”, “cherry”)) | tuple |
x = range(6) | range |
x = dict(name=”John”, age=36) | dict |
x = set((“apple”, “banana”, “cherry”)) | set |
x = frozenset((“apple”, “banana”, “cherry”)) | frozenset |
x = bool(5) | bool |
x = bytes(5) | bytes |
x = bytearray(5) | bytearray |
x = memoryview(bytes(5)) | memoryview |