Click for Python Data Types (Part 1)
Python Data Types
Continue this tutorial about Python Data Types. In the previous tutorial we already learned about Text Data Type and Sequence Data Type. Now continue with the rest of the data types.
Numeric Data Types in Python
int, float, and complex are considered numeric data types in Python. All three data types are used to represent different kinds of numbers in Python.
Int Data Types in Python
The int data type is used for integer numbers. Integer numbers are whole numbers. Positive and negative numbers are also considered integer numbers but decimal and fraction numbers are not integers. For example:
Code:
num1 = 100
num2 = -120
num3 = 0
print(type(num1))
print(type(num2))
print(type(num3))
Output:
<class 'int'>
<class 'int'>
<class 'int'>
So we get the 'int'
class in the output. See the below example to understand the difference between string and int data types.
Code:
num1 = '100'
num2 = 100
print(type(num1))
print(type(num2))
Output:
<class 'str'>
<class 'int'>
In num1
we write a number as a value but surrounded by quotes. And in num2
, we simply write a number. We get the 'str'
for first and 'int'
class for second. So even if we write a number but using quotes, then it is a string.
Float Data Types in Python
A floating point number or float, are the decimal numbers. All decimal numbers, positive and negative, are considered as float data types. For example:
Code:
num1 = 4.5
num2 = -7.7
print(type(num1))
print(type(num2))
Output:
<class 'float'>
<class 'float'>
We get 'float'
class in the output. If we write numbers like this:
Code:
num1 = 5/2
num2 = -9/6
print(type(num1))
print(type(num2))
Output:
<class 'float'>
<class 'float'>
We get the 'float'
class in the output. So decimal values are related to the float class in Python.
Complex Data Type in Python
Complex numbers are related to complex classes in Python. Complex numbers are made by two parts, real and imaginary. For example, x + yj is a complex number. Here x is a real part and y is an imaginary part. We always add character j with the imaginary part. Like, 4+3j is an example of a complex number. A complex number could be positive, negative, or decimal numbers.
Code:
num1 = 4+2j
num2 = -2+5j
num3 = 2.3+6j
num4 = 9j
num5 = 9-5j
print(type(num1))
print(type(num2))
print(type(num3))
print(type(num4))
print(type(num5))
Output:
<class 'complex'>
<class 'complex'>
<class 'complex'>
<class 'complex'>
<class 'complex'>
So this is the complex data type in Python.
Boolean Data Type
The boolean data type is a built-in data type in Python. It represents two values, True and False. Here True means one (1) and False means zero (0). Mostly we used boolean data type when we wanted to evaluate an expression. For example:
Code:
value1 = True
value2 = False
print(type(value1))
print(type(value2))
Output:
<class 'bool'>
<class 'bool'>
The boolean data type is represented as 'bool'
class.
Code:
num1 = 5 > 2
num2 = 5 > 8
print(num1)
print(num2)
print(type(num1))
print(type(num2))
Output:
True
False
<class 'bool'>
<class 'bool'>
Set Data Types in Python
set and frozenset are considered as set data types in Python.
Set Data Type in Python
Set is just like lists and tuples. It is used to store collections of data in Python. However, set properties are different from lists and tuples. Set is an unordered, mutuable kind of data type. We cannot use duplicate values in a set. We use curley brackets {} to create sets in Python. For example:
Code:
my_set = {'Apple', 'Python', 14, 52}
print(my_set)
print(type(my_set))
Output:
{'Python', 52, 'Apple', 14}
<class 'set'>
In the output, we get the class 'set'
. Also in the output, we didn’t get the values in the same order in which we write in set. Because set is an unordered kind of data type.
Frozenset Data Type in Python
Frozenset is like set but it is an immuatable collection of unique values. We use frozenset to convert the mutable data type into an immutable data type. We use the built-in function frozenset() to create the frozenset in Python. For example:
Code:
first = frozenset({'Apple', 'Python'})
print(first)
print(type(first))
Output:
frozenset({'Apple', 'Python'})
<class 'frozenset'>
This is how we create frozenset. It has separated class 'frozenset'
.
Dictionary Data Type in Python
A dictionary is used to hold data collection, just like a list, tuple, and set. But in a dictionary, data is stored in a key-value pair. We also use curly brackets {} to create a dictionary. We use a comma to separate values and a semicolon to separate the key and value. For example:
Code:
num_dict = {1:'One', 2:'Two', 'Name':'Python'}
print(num_dict)
print(type(num_dict))
Output:
{1: 'One', 2: 'Two', 'Name': 'Python'}
<class 'dict'>
‘dict’ class indicated dictionary data type. Dictionary is also considered a mapping data type in Python.
None Data Type in Python
None is a different kind of data type in Python. None represent no value or null values. If a function doesn’t return a value, it returns None. In other programming languages, null, nill, undef, and undefined are used instead of None. In Python, there is nothing like null.
None doesn’t represent zero, False, or empty string. None has a separated data type in Python. For example:
Code:
first =
second = 100
print(second)
Output:
SyntaxError: invalid syntax
Here we created two variables but didn’t assign any value to the first variable. We cannot leave an empty statement like this. So instead of leaving it’s empty, we use None.
Code:
first = None
second = 100
print(second)
Output:
100
<class 'NoneType'>
So this is the use of None data type. And we can see, it has a separated data type. We get class 'NoneType'
in the output.
Binary Data Types in Python
Computers only understand binary language. Binary language has only two binary digits, zero (0) and one (1). We use binary data types to represent the data into binary digits. Binary data types use a combination of 0s and 1s to represent the information. Python has three kinds of binary data types. bytes, bytearray, memoryview
Bytes data type in Python
The sequence of bytes is represented by byte data type. Every byte consists of an integer value between 0 and 255. Bytes are used to store images and files in the binary format. We can create byte data type by using the built-in function bytes()
. For example:
Code:
num = bytes(100)
print(type(num))
Output:
<class 'bytes'>
We get 'bytes'
class in the output. Here we are converting an integer value (100) into a binary digit. There is another way to write bytes data type. If we simply write character b before any value.
Code:
num1 = '100'
num2 = b'100'
print(type(num1))
print(type(num2))
Output:
<class 'str'>
<class 'bytes'>
So this is how we can create a bytes data type in Python.
Bytearray Data Type in Python
Bytearray data type is the same as bytes data type. The main difference between both of them is that bytes create immutable objects, while bytearray create mutable objects. We use the bytearray() function to create bytearrray data type.
Code:
num = bytearray(5)
print(type(num))
print(num)
Output:
<class 'bytearray'>
bytearray(b'\x00\x00\x00\x00\x00')
We get ‘bytearray’ class in the result. Here we convert an integer value into a binary format.
Memoryview Data Type in Python
If we want to view the memory of an object, we used the memoryview data type. However, it is only possible if that object is supported by buffer protocol. For example:
Code:
num1 = bytes(1)
num2 = memoryview(num1)
print(type(num2))
print(num2)
Output:
<class 'memoryview'>
<memory at 0x000001F8A8395A80>
So here we used the memoryview data type to view the memory of a bytes object.
Conclusion
So this is the end of the Python data types tutorial. These are the basic and most used data types in Python. There are more but these are the basic and much-needed data types to write Python code. You don’t need to remember data type by name, especially when Python is a dynamically typed language. We don’t have to specify the data type at the time of creation. But it is important to understand different kinds of data types.
End of Python Data Types (Part 2)
Click for the tutorial on type() and isinstance() functions in Python
One thought on “Python Data Types (Part 2): What is a data type in Python?”