Table of contents
Introduction
Arrays are fundamental data structures used in programming to store and manipulate collections of elements. They provide efficient and organized ways to manage data, making them essential in various applications. An array is a contiguous block of memory that stores a fixed-size collection of elements of the same type. Each element in the array is identified by its index, which represents its position within the array. Arrays can be one-dimensional (1D), two-dimensional (2D), or multidimensional, depending on the number of indices required to access the elements.
Operations
Accessing Elements: Array elements can be accessed using their respective indices. In Python, array indexing starts from 0, so the first element of an array is at index 0.
Modifying Elements: Elements within an array can be modified by assigning new values to specific indices.
Array Length: The length of an array refers to the total number of elements it contains. This information is crucial for iterating through arrays.
Array Traversal: Traversing an array involves visiting each element in the array, usually done through loops such as "for" or "while" loops.
Array Insertion and Deletion: Arrays allow the insertion and deletion of elements at various positions. Insertion typically involves shifting existing elements to accommodate the new element, while deletion involves removing an element and adjusting the array accordingly.
Searching and Sorting: Arrays can be searched to find specific elements or sorted to arrange the elements in a particular order, such as ascending or descending.
Implementation
Python provides built-in data structures and libraries that facilitate working with arrays.
Array Creation
To create an array, specify the type code (e.g., 'i' for integers, 'f' for floats) and pass the elements as arguments
import array my_array = array('i', [1, 2, 3, 4, 5])
Array Operations
Accessing Elements
Elements can be accessed using their indices.
print(my_array[0]) # Output: 1
Modifying Elements
Elements can be modified by assigning new values to specific indices.
my_array[2] = 6
Array Length
Obtain the length of an array using the
len()
function.length = len(my_array)
Array Traversal
Iterate through the array using a loop.
for element in my_array: print(element)
Array Insertion and Deletion
- Python provides several methods to insert or delete elements from arrays, such as
insert()
,append()
,extend()
,remove()
, etc.
- Python provides several methods to insert or delete elements from arrays, such as
Searching and Sorting
Arrays can be searched using the
index()
method and sorted using thesort()
method.
Real-Time Example
Let's consider a real-time example of using arrays in Python to calculate student grades. Suppose we have an array that stores the marks obtained by students in a particular subject. We can perform operations like finding the average, highest and lowest marks, and identifying students who scored above a certain threshold.
# Array of student marks
marks = array('i', [85, 92, 78, 95, 88])
# Finding average marks
average = sum(marks) / len(marks)
print("Average Marks:", average)
# Finding the highest and lowest marks
highest = max(marks)
lowest = min(marks)
print("Highest Marks:", highest)
print("Lowest Marks:", lowest)
# Students above a certain threshold
threshold = 90
above_threshold = [mark for mark in marks if mark > threshold]
print("Students above", threshold, "marks:", above_threshold)