There are 5 Python fundamental data types also called as explicit type casting data types or simply type casting data types. In this tutorial today we will learn more about the nature, mutable or immutable, etc of these python fundamental data types. So let’s get started by naming them first. They are:-
- Int
- Float
- Complex
- Bool
- Str
- Although searching throughout for a suitable object might be time-consuming but it still remains less costly when compared to creating an object.
- The reason behind the range is restricted to 0 to 256 is that many anomalies creep up when we go beyond this.
- Even before we write our program objects for 0 to 256 are created at the start of Python Virtual Machine and Interpreter.
- 0 to 256 is chosen as the range as it is the most commonly used range.
- In case of boolean, the same object is reused always as it need not be changed.
- For float, reusability cannot be applied as there is an infinite number of values between every number and obviously infinite number of objects cannot be created by the interpreter beforehand.
- For the same reason, this concept is also not implemented on complex numbers.
- In case of string literals, the objects are not created beforehand by the interpreter.
Bytes:
This data type represents a collection of byte numbers similar to an array.
- The range of values that it can hold is 0 to 256 on exceeding which we will get a ValueError.
- It is used mostly while representing binary data like images and video files.
Example:
#declaring variable 'a' >>>a= [1, 2, 3, 4, 5] >>>b= bytes(a) #printing type of variable >>>type(b)
Output: <class ‘bytes’>
In the program above we create a bytes data type as we can check by using the type() function.
-We can use the index numbers in a similar way to the strings to print the various numbers.
Example:>>>b[0] #we access the element at index 0
Output:1
Example:>>>b[1] #we access the element at index 1
Output:2
In these examples above we simply use the variable name of byte data type already created and use the index number i.e. 0 and then 1 to get the elements from respective indexes.
-To print all the numbers in the bytes data type all at once then we use ‘for’ :-
Example:-
>>> for a in b: print(a) #running a for loop
Output:
#we obtain the output in a series 1 2 3 4 5
Here in the example above we use the for loop to print all the elements of the bytes data type variable in a sequence.
- Bytes data type is immutable i.e. non-changeable. It does not support value assignment.
Example:
>>>a= [1, 2, 3, 4, 5] >>>b= bytes(a) #assigning element at 0 with a value >>>b[0]= 5
Output:ValueError: byte value assignment not possible
In the above code, we create a bytes variable and then try to assign its element at index 0 with a value. This returns an error as value assignment is not possible in bytes due to its immutable nature.
Bytearray:
Bytearray is completely same as bytes but it is mutable, unlike bytes.
Example:
>>>a= [1, 2, 3, 4, 5] >>>b= bytearray(a) #creating variable of bytearray type >>>type(b) <class ‘bytearray’>
Here, we create a bytearray data type ‘b’ and assign values 1, 2, 3, 4 and 5 to it.
To print all the numbers in the bytes data type all at once then we use ‘for’ loop :-
Example:-
>>>for i in b: print(i)
OUTPUT:
1 2 3 4 5
Here we use the for loop to print all the elements of the bytearray data type variable in a sequence.
- It is changeable. But in the range of 0 to 256.
Example:
>>>a= [1, 2, 3, 4, 5] >>>b= bytes(a) #assigning element at 0 with a value above 256 >>>b[0]= 365
Output: ValueError: byte must be in the range(0, 256)
list:
- In the list data type, the order of insertion is preserved and duplicates are allowed. It is declared using square brackets.
- Also, it is heterogeneous i.e. multiple data types are allowed to be inserted.
Example:
#list 'a' is created >>> a= [] >>> type(a) <class ‘list’> #we add to the list using append >>> a.append(1) >>> a.append(2) >>> a.append(3) >>> a.append(1) >>> print(a)
Output: [1, 2, 3, 1]
Through this piece of code, we can see that the order of insertion has been preserved and the inputs can be repeated i.e. duplicates are allowed. Here a list is declared with a variable name ‘a’. We then add elements to the list using append() one by one and finally print the list.
Now if we want to add other inputs then we can do that as well since the list data type is heterogeneous.
To the above code if we add these next few lines, then,
>>> a.append(‘meow’) #adding 'Meow' to 'a' >>> print(a)
Output: [1, 2, 3, 1, ‘meow’]
To the previous example which had only numbers, we add a string literal ‘meow’.
- Like the bytes and bytearray data type in the list also we can print each individual element using the index numbers.
Example:>>> a[0]
Output: 1
- The slice operator can be applied on lists in a way similar to strings.
Example:>>> a[1:5]
Output: [2, 3, 1, ‘meow’]
In this example, we apply the slice operator from index 1 to 5. And the output is the elements from index 1 to 5-1 i.e.
- For repeating the values up to n number of times we can make use of the ‘*’ operator followed by the list name.
Example:-
>>> l= [1, 2, ‘Meow’, True] #we create the list l >>> l1= l*2 #l1 will store duplicated elements of l >>> l1
Output:[1, 2, ‘Meow’, True, 1, 2, ‘Meow’, True]
Here a list is declared with a variable name ‘l’. We then add elements to the list one by one and take another variable ‘l1’ which will contain the value of ‘l’ duplicated.