Learn About the Different Data Types in Python Programming

Data Types in Python
A data type is an important concept not only in Python Programming but also in general programming.
Data stored in memory can be of different types and each performs distinct functions.
For example, the name of a person is stored as a different type, while the age is of a different type.
Some programming languages like Java, C, and C++ practice "type declaration" (explicitly stating the type of the variable before assigning a value).
In python programming, there's no type declaration. The type of the variable is automagically detected through the assigned value.
Standard Built-in Data Types in Python
There are different standard data types in python programming. These data types are built into python.
Python has the following data types built-in by default:
Data Type | Full Name | Uses | Example |
---|---|---|---|
str | String | For text and alphanumeric values. | var = "Archangel Macsika" |
int | Integer | For numeric values. | var = 15 |
float | Floating-Point | For decimal values. | var = 15.3 |
complex | Complex | For complex numeric values. | var = 9.314e-53j |
list | List | For values that are in sequence. | var = ["Monday", "Tuesday", "Wednesday"] |
tuple | Tuple | For values that are in sequence. | var = ("Monday", "Tuesday", "Wednesday") |
range | Range | For values that are in sequence. | var = range(6) |
dict | Dictionary | For values that involve mapping. | var = {"name" : "Archangel", "age" : 24} |
set | Set | For values in set format. | var = {"Monday", "Tuesday", "Wednesday"} |
frozenset | Frozenset | For values in Set Format | var = frozenset({"Monday", "Tuesday", "Wednesday"}) |
bool | Boolean | For values in boolean format (True or False). | var = True |
bytes | Bytes | For binary type of values. | var = b"Archangel" |
bytearray | Bytearray | For binary type of values in array. | var = bytearray(7) |
memoryview | Memoryview | For binary type of values. | var = memoryview(bytes(7)) |
Getting the Data Type in Python
Using the type()
function, you can get the data type of any value or object:
For example:
full_name = "Archangel Macsika"
print(type(x))
Output:
<class 'str'>
Data Type Conversion and Casting in Python
Sometimes, you may want to explicitly state the data type or convert from one data type to another. For instance, change a list
data type to a set
.
There are several built-in functions in python to help you achieve this. And after conversion, the object returned is the new converted object.
For example:
var = ["Monday", "Tuesday", "Wednesday"]
print(type(var))
var = set(var)
print(type(var))
Output:
In the output below, the list
data type is the original data type of the var
, whereas the set
data type is the newly converted one.
<class 'list'> <class 'set'>
Here are other data types and their functions used to either explicitly set or convert to another data type:
Data Type | Function | Example |
---|---|---|
str | str(x) | var = str("Archangel Macsika") |
int | int(x[,base]) | var = int(15) |
float | float(x) | var = float(15.3) |
complex | complex(real[,imag]) | var = Complex(9.314e-53j) |
list | list(x) | var = list(("Monday", "Tuesday", "Wednesday")) |
tuple | tuple(x) | var = tuple(("Monday", "Tuesday", "Wednesday")) |
range | range(x) | var = range(6) |
dict | dict(x) | var = dict("name" : "Archangel", "age" : 24) |
set | set(x) | var = set(("Monday", "Tuesday", "Wednesday")) |
set | frozenset(x) | var = frozenset(("Monday", "Tuesday", "Wednesday")) |
bool | bool(x) | var = bool(4) |
bytes | bytes(x) | var = bytes(4) |
bytearray | bytearray(x) | var = bytearray(7) |
memoryview | memoryview(x) | var = memoryview(bytes(7)) |
Wrap Off
Data type is an important concept in Python Programming. They come in different types and perform different functions.
If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.