Python Data Types
Learn about the different data types available in Python and how to use them.
Built-in Data Types
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:
Text Type
str Numeric Types
int, float, complex Sequence Types
list, tuple, range Mapping Type
dict Set Types
set, frozenset Boolean Type
bool Binary Types
bytes, bytearray, memoryview None Type
NoneType Getting the Data Type
You can get the data type of any object by using the type() function:
Example
x = 5
print(type(x)) Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
Example
x = "Hello World" Data Type: str
Example
x = 20 Data Type: int
Example
x = 20.5 Data Type: float
Example
x = 1j Data Type: complex
Example
x = ["apple", "banana", "cherry"] Data Type: list
Example
x = ("apple", "banana", "cherry") Data Type: tuple
Example
x = range(6) Data Type: range
Example
x = {"name" : "John", "age" : 36} Data Type: dict
Example
x = {"apple", "banana", "cherry"} Data Type: set
Example
x = frozenset({"apple", "banana", "cherry"}) Data Type: frozenset
Example
x = True Data Type: bool
Example
x = b"Hello" Data Type: bytes
Setting the Specific Data Type
If you want to specify the data type, you can use the following constructor functions:
Example
x = str("Hello World") Data Type: str
Example
x = int(20) Data Type: int
Example
x = float(20.5) Data Type: float
Example
x = complex(1j) Data Type: complex
Example
x = list(("apple", "banana", "cherry")) Data Type: list
Example
x = tuple(("apple", "banana", "cherry")) Data Type: tuple
Example
x = range(6) Data Type: range
Example
x = dict(name="John", age=36) Data Type: dict
Example
x = set(("apple", "banana", "cherry")) Data Type: set
Example
x = frozenset(("apple", "banana", "cherry")) Data Type: frozenset
Example
x = bool(5) Data Type: bool
Example
x = bytes(5) Data Type: bytes