Code Explanation:
1. Importing the array module
import array
The array module provides an array data structure which is more memory-efficient than Python’s built-in lists.
Here, we’ll use it to store integers compactly.
2. Creating an integer array
arr = array.array('i', [1, 2, 3])
array.array('i', [...]) creates an array of integers ('i' = type code for signed integers).
The initial array is:
arr = [1, 2, 3]
3. Appending an element
arr.append(4)
.append(4) adds 4 at the end of the array.
Now array becomes:
[1, 2, 3, 4]
4. Removing an element
arr.remove(2)
.remove(2) removes the first occurrence of 2 from the array.
Now array becomes:
[1, 3, 4]
5. Printing values
print(len(arr), arr[1], arr[-1])
len(arr) → gives number of elements → 3.
arr[1] → second element (indexing starts at 0) → 3.
arr[-1] → last element → 4.
Final Output:
3 3 4
.png)

0 Comments:
Post a Comment