In this tutorial, we will extend our knowledge on Data types in Python 3. Apart from the fundamental data types, These are the extended data types that are generally used to solve real-world problems.
bytes Data Type in Python 3
- It represents a group of byte numbers.
- It is an Immutable data type. It means, we can’t change it after creating it. So, it doesn’t support value assignment or insertion. e.g. b = 12
- Every value in bytes data type should lie between 0 to 256.
- We use this data type to store images, video files, or to make communication with Bluetooth devices. We generally send data in bytes while communicating with Bluetooth using our program.
x = bytes([2, 3, 4, 192]) print(x) # It will print # b'\x02\x03\x04\xc0'
Byte Array Data Type – Bytes vs Byte Array in Python 3
- This data type is similar to Bytes. It has only one difference. It supports mutability. So, we can easily assign or insert value using an index as well.
x = bytearray([2, 3, 4, 192]) x = 99 # It works in Byte Array for i in x: print(i)
Overview List Data Type in Python 3
- In python 3, List is used to store a group of values as a single entity.
- It preserves its order. The order in which it is created is preserved.
- Unlike traditional arrays, List supports heterogeneous values. We can store string and int objects in the same list object.
- There is no limit of size in List. We can grow the list and shrink it as per our requirements.
l = [12, 32, 72, "valid"] # Heterogeneous values allowed print(l)
Tuple Data Type in Python 3
- List Data type is Mutable. It means we can change it at any time. If we want immutability in our list. Then we can use Tuple.
- There is only one difference between List and tuple in Python 3. Tuple supports immutability while List supports mutability.
t = (10, 32, 34) print(t)
I hope you will learn something new from this article on Bytes, Byte Array, List, and Tuple Data Type in Python 3. If you have some suggestions or doubts regarding this article then you can comment below your opinion.