In programming, data type is an important concept. Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
strint, float, complexlist, tuple, rangedictset, frozensetboolbytes, bytearray, memoryviewNoneTypeYou can easily get the data type of any object by using the built-in type() function.
x = 5 y = "Hello, World!"print(type(x)) print(type(y))
In Python, the data type is automatically set when you assign a value to a variable.
a = "Hello" # str
b = 20 # int
c = 20.5 # float
d = ["a", "b"] # list
e = {"name": "A"} # dict
f = True # bool
print(type(b))
print(type(d))
If you want to manually specify the data type, you can use the constructor functions (like str(), int(), etc.), which we will explore further in the Python Casting chapter.
x = str("Hello World")
y = int(20)
Which built-in function can be used to find the data type of a variable in Python?