Python Data Types
A Data type is the type of value a variable can store.
Python is a dynamically typed programming language, which means that the data type of a variable does not need to be declared. A variable data type is determined at runtime. However, you must be aware of the data types supported by Python.
Python has the following built-in data types:
Data Type | Description | Example |
---|---|---|
str | A string which is a sequence of character. | x = "Hello World" |
int | Numeric value without decimal point. | a = 467 |
float | Numeric value with decimal point. | 14.7 |
complex | Numeric real value with imaginary component. | x = 14j |
list | Collection of ordered and mutable data. This type allows duplicate values. | x = ["apple", "grapes", "mango", "orange", "apple"] |
tuple | Collection of ordered and immutable data. This type allows duplicate values. | x = ("apple", "grapes", "mango", "orange", "mango") |
dict | A collection with key-value pairs which are unordered, unindexed and mutable. This type allows no duplicate keys. | a = {"name" : "Danny", "email" : "[email protected]"} |
set | A collection with unindexed and unordered values. This type allows no duplicate values. | a = {"pen", "book", "laptop"} |
frozenset | A built-in function to make iterable object immutable. | frozenset({"pen", "book", "laptop"}) |
bytes | A byte representation of an object value. | a = b"Hello World" |
bytearray | A built-in function to convert object's value into bytearray. | x = bytearray(10) |
memoryview | A built-in function to convert object into memoryview object. Memoryview allows to safely access the internal buffer of an object in Python. Internal buffer refers to a physical memory storage used temporarily to store data. | x = memoryview(bytes(10)) |
bool | A boolean value either True or False. | x = True |
range | A built-in function that generates a sequence of numbers starting with 0 by default and stops just before the specified number. It is commonly used in for loop. |
for i in range(10):
print(i, end = " ") print() |
Declaring Data Type
Python variables don't need to have their data types declared explicitly. The data type of a variable is determined by the kind of value assigned to it.
Example:
a = "Hello World!"
print(a, " is of type ", type(a).__name__)
a = 100
print(a, " is of type ", type(a).__name__)
a = 100.34
print(a, " is of type ", type(a).__name__)
a = 100j
print(a, " is of type ", type(a).__name__)
a = [10, 45, 23, 78, 19, 20, 14]
print(a, " is of type ", type(a).__name__)
a = (100, 503, 45)
print(a, " is of type ", type(a).__name__)
a = {"name":"Danny", "age": 27}
print(a, " is of type ", type(a).__name__)
a = {"apple", "grapes", "mango"}
print(a, " is of type ", type(a).__name__)
a = frozenset({56, 22, 17})
print(a, " is of type ", type(a).__name__)
a = b"Hello world!"
print(a, " is of type ", type(a).__name__)
a = bytearray("Hello world!", "utf-8")
print(a, " is of type ", type(a).__name__)
a = memoryview(bytes("Hello world!", "utf-8"))
print(a, " is of type ", type(a).__name__)
a = True
print(a, " is of type ", type(a).__name__)
a = range(10)
print(a, " is of type ", type(a).__name__)
The output of the above example is:
100 is of type int
100.34 is of type float
100j is of type complex
[10, 45, 23, 78, 19, 20, 14] is of type list
(100, 503, 45) is of type tuple
{'name': 'Danny', 'age': 27} is of type dict
{'mango', 'grapes', 'apple'} is of type set
frozenset({56, 17, 22}) is of type frozenset
b'Hello world!' is of type bytes
bytearray(b'Hello world!') is of type bytearray
<memory at 0x7fc3e57a5280> is of type memoryview
True is of type bool
range(0, 10) is of type range
Declaring Specific Data Type
In Python, we can also specify the data type of a variable using the constructor functions as shown in the example below:
a = str("Hello World!")
a = int(100)
a = float(100.34)
a = complex(100j)
a = list((10, 45, 23, 78, 19, 20, 14))
a = tuple((100, 503, 45))
a = dict({"name":"Danny", "age": 27})
a = set(("apple", "grapes", "mango"))
a = frozenset({56, 22, 17})
a = bytes("Hello world!", "utf-8")
a = bytearray("Hello world!", "utf-8")
a = memoryview(bytes("Hello world!", "utf-8"))
a = bool(0)
a = range(10)
Choosing the Right Data Types
The right data type for a variable not only assures that a program runs correctly, but it also improves code efficiency. If you want to add the values of two variables, then both variables must be integer. You can't expect 2 by adding 1 + "1" because 1 is an integer and "1" is a string.
The following example gives Type Error because we are attempting to add an integer and a string variable:
Example:
a = 1
b = "1"
c = a + b
print(c)
The output of the above example is:
The correct way of adding two integer variables is:
a = 1
b = 1
c = a + b
print(c)
The output of the above example is:
How to find the data type of an object or a variable?
To determine the data type of an object or variable, use Python's type() function.
Example:
x = 67
print(type(x))
The output of the above example is: