Data Type represent the type of data present inside a variable. In Python, you are not required to specify the data type explicitly. Based on value provided, data type will be assigned automatically. For instance, a person’s age is stored as a numeric value and his or her address is stored as alphanumeric characters. Hence Python is Dynamically Typed Language.
Python contains the following built-in data types.
Example | Data Type |
Text Type | str |
Numeric Types | int, float, complex |
Sequence Types | list, tuple, range |
Mapping Type | dictionary (also known as dict) |
Set Types | set, frozenset |
Boolean Type | bool |
Binary Types | bytes, bytearray, memoryview |
Setting the Data Type
In Python, the data type is set by default when you assign a value to a variable.
Example | Data Type |
x = “Hello World” | str |
x = 10 | int |
x = 10.5 | float |
x = 10j | complex |
x = [“Amar”, “Akbar”, “Anthony”] | list |
x = (“Amar”, “Akbar”, “Anthony”) | tuple |
x = range(5) | range |
x = {“name” : “Ravi”, “age” : 26} | dict |
x = {“Amar”, “Akbar”, “Anthony”} | set |
x = frozenset({“Amar”, “Akbar”, “Anthony”}) | frozenset |
x = True | bool |
x = b”Hello” | bytes |
x = bytearray(10) | bytearray |
x = memoryview(bytes(10)) | memoryview |
Setting the Specific Data Type
If you want to set the specific data type manually, you can use the following constructor.
Example | Data Type |
x = str(“Hello World”) | str |
x = int(10) | int |
x = float(10.5) | float |
x = complex(10j) | complex |
x = list((“Amar”, “Akbar”, “Anthony”)) | list |
x = tuple((“Amar”, “Akbar”, “Anthony”)) | tuple |
x = range(5) | range |
x = dict(“name” : “Ravi”, “age” : 26) | dict |
x = set((“Amar”, “Akbar”, “Anthony”)) | set |
x = frozenset((“Amar”, “Akbar”, “Anthony”)) | frozenset |
x = bool(10) | bool |
x = bytes(10) | bytes |
x = bytearray(10) | bytearray |
x = memoryview(bytes(10)) | memoryview |
Getting the Data Type
You can get the data type by using the type() function.
x = 5
print(type(x))
#output
<class 'int'>
So far we have described the data type used in Python programming language. I hope you got a better understanding of Built-in data types in python.
Previous Read : Python Variables
1 thought on “Python Data Types”