Python Data Types

Python Data Types: Built-in Types Explained

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:


Getting the Data Type

You can easily get the data type of any object by using the built-in type() function.

Using the type() function

x = 5
y = "Hello, World!"

print(type(x)) print(type(y))


Setting the Data Type

In Python, the data type is automatically set when you assign a value to a variable.

Implicit Type Assignment

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))

Setting the Specific Data Type

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)

Exercise

?

Which built-in function can be used to find the data type of a variable in Python?