ARRAY
An array is like a list of items such as numbers or data, where each item is placed in a particular order and labeled with a unique number called an index. This arrangement makes it simple to find and work with individual items by using their index. Arrays are a fundamental tool in computer programming for organizing and handling data effectively, enabling easy access, modification, and looping through the items.
Basic Operation
- Access
- Insertion
- Deletion
- Search
- Update
Access
Access is a fundamental operation when working with arrays because it allows to retrieve, use, or manipulate the data stored in the array based on the order and position of the elements.
Example
my_array = [1, 2, 3, 4, 5]
# Accessing elements by index
first_element = my_array[0] # Accesses the element at index 0 (which is 1)
second_element = my_array[2] # Accesses the element at index 2 (which is 3)
# Printing the values of the accessed elements
print("First element:", first_element)
print("Second element:", second_element)
OUTPUT
First element: 1
Second element: 3
Insertion
Insertion refers to adding new elements to the array at specific positions or at the end.
Its a common operation when we need to expand or modify the contents of an array by adding new data.
Example
my_array = [1, 2, 3, 4, 5]
# Accessing elements by index
first_element = my_array[0] # Accesses the element at index 0 (which is 1)
second_element = my_array[1] # Accesses the element at index 1 (which is 2)
# Printing the values of the accessed elements
print("First element:", first_element)
print("Second element:", second_element)
OUTPUT
First element: 1
Second element: 2
Deletion
Deletion refers to removing of an element from the array at the specified index.
Its a common operation when we need to remove unwanted data from an array to keep it organized and maintain its integrity.
Example
my_array = [1, 2, 3, 4, 5]
# Deleting an element at a specific index (index 2)
del my_array[2]
OUTPUT
[1, 2, 4, 5]
Search
Search refers to the process of looking for a specific element within the array and returns its index if it is found.
Its a common operation when we want to find, locate, or identify elements within an array, which is a fundamental part of many data processing and retrieval tasks in programming.
Example
my_array = [1, 2, 3, 4, 5]
# Element to find
old_value = 3
# New value to replace the old_value
new_value = 6
if old_value in my_array:
index = my_array.index(old_value)
my_array[index] = new_value # Replace the element at the found index
print(f"The element {old_value} has been replaced with {new_value}.")
else:
print(f"The element {old_value} is not in the array.")
OUTPUT
The element 3 has been replaced with 6.
Update
Update refers to the process of changing the value of an element at the specified index within the array.
Its a common operation when we need to modify or refresh the data stored in an array to reflect changes or new information.
It allows to keep the array's contents up to date.
Example
my_array = [1, 2, 3, 4, 5]
# Updating an element at a specific index (index 2)
my_array[2] = 30
OUTPUT
[1, 2, 30, 4, 5]