0% completed
Arrays in Python are a collection of elements of the same type, stored in contiguous memory locations. Unlike lists, which can store elements of different types, arrays in Python are designed to handle homogeneous data types, making them more efficient for certain types of numerical computations and data storage tasks.
Arrays are particularly useful when dealing with large volumes of data that require performance-sensitive handling. In Python, arrays can be handled using the array
module, which provides the necessary functionality to perform operations like traversals, insertions, deletions, searches, and updates efficiently.
To work with arrays in Python, you first need to import the array
module. Here's how you can define an array:
'byte_code'
is the type code that defines the type of array elements.[ele1, ele2, ele3, ...]
represents the array of elementsWhen creating an array, the type code is crucial as it directly influences the physical size in memory and the operations allowed on the array. Here is a table summarizing common type codes:
Type Code | C Type | Python Type | Minimum Size (Bytes) |
---|---|---|---|
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'u' | Py_UNICODE | Unicode character | 2 (or 4) |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | int | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | int | 4 |
'f' | float | float | 4 |
'd' | double | float | 8 |
Array indexing is a fundamental concept in Python, as it allows you to access, modify, and manipulate elements directly by referring to their position within the array.
Each element in an array is assigned a unique index, which represents its position. These indices start at 0 for the first element and increase by 1 for each subsequent element.
Here's a breakdown of how indexing works in arrays and how you can use it to access elements:
Positive Indexing: This is the most direct way to reference an element. The index starts at 0 for the first element, 1 for the second element, and so on up to n-1
for the last element, where n
is the number of elements in the array.
Negative Indexing: Python also supports negative indexing for arrays, which allows you to access elements from the end of the array backwards. Here, -1
refers to the last element, -2
to the second last, and so on.
Here's a simple example to illustrate both positive and negative indexing in a Python array:
Explanation:
a[0]
and a[1]
: These are examples of positive indexing. a[0]
accesses the first element (10
), and a[1]
accesses the second element (20
).a[-1]
and a[-2]
: These illustrate negative indexing. a[-1]
accesses the last element (50
), and a[-2]
accesses the second last element (40
).Traversing an array involves accessing each element in sequence and performing an operation such as printing. This is fundamental for displaying the array's contents or for further processing of its elements.
Here, we will print each element of an integer array.
Explanation:
arr
for ease of use.a
with the type code 'i'
, indicating the elements are integers. The array is initialized with the values 1, 2, 3, 4, and 5.for
loop: Iterates through each element in the array a
. The print
function outputs each element followed by a space, thanks to end=' '
, which overrides the default newline end character.Insertion involves adding an element at a specific index. This operation shifts subsequent elements to the right, modifying the array's structure.
In this example, we will insert the number 10
at the second position of the array.
Explanation:
10
at index 1
. The existing elements from that position onward are shifted right.Deletion removes an element at a specified index, which shifts subsequent elements to the left.
In this example, we will remove the element at the second position from the array.
Explanation:
a.pop(1)
: Removes the element at index 1
, which is 10
, from the array a
.You can search for an element by its value to retrieve its index in the array.
In this example, we will find the index of the element 3
in the array.
Explanation:
3
in the array a
and returns its index, which is 2
.Updating an array involves changing the value of an element at a specified index.
In this example, we will change the third element to 20
in the array.
Explanation:
2
(originally 3
) to 20
.These examples comprehensively demonstrate basic operations on arrays in Python, showing how to manipulate array data effectively. These operations form the foundation for working with arrays in applications that require efficient data handling.
.....
.....
.....