Understanding Arrays in Python and Their Operations | FACE Prep
Arrays in Python: Comprehensive Guide with Examples
Arrays in Python are a specialized data structure used to store multiple elements of the same data type in a single variable. Unlike lists, which can hold elements of varying data types, arrays enforce type consistency, making them more efficient in certain use cases.
Why Do We Need Arrays?
Arrays are particularly useful for scenarios where you need to store and manage a large number of elements of the same type. For example, imagine you need to store 100 integers. Creating 100 variables like this:num1 = 23num2 = 36num3 = 41# …up to num100 = 92is not only tedious but also inefficient. Instead, you can use an array to store all these values in a single structure, allowing for streamlined operations and reduced code complexity.
Creating an Array in Python
In Python, you can create arrays using the array() method from the built-in array module. This method requires two arguments:
The data type of the array elements.
The list of elements to be included in the array.
Syntax
from array import *array_name = array(‘datatype’, [array_elements])
Example
from array import *arr = array(‘i’, [2, 3, 5, 6])In this example, an array of integers is created with the data type specified as ‘i’ (for integers). Below is a table of commonly used data type codes:
Data Type
Code
Integer
‘i’
Float
‘f’
Double
‘d’
Unicode
‘u’
Accessing Array Elements
Array elements can be accessed using their index value, starting from 0 for the first element. Here’s an example:from array import *arr = array(‘u’, [‘F’, ‘A’, ‘C’, ‘E’])print(arr[1])Output:A
Modifying Array Elements
You can modify an element in an array by assigning a new value to a specific index. Only the targeted index is updated, leaving the rest of the array unchanged.
Using a for loop, you can easily iterate over all elements in an array and print them:from array import *arr = array(‘i’, [10, 20, 30])for x in arr: print(x, end=’ ‘)Output:10 20 30
Adding Elements to an Array
To add new elements to an array, use the append() method. Note that this method can only add a single element at a time.
Example
from array import *arr = array(‘i’, [10, 20, 30])arr.append(40)for x in arr: print(x, end=’ ‘)Output:10 20 30 40
Deleting Elements from an Array
Python provides two methods to delete elements from an array:
a) Using pop()
The pop() method removes an element by its index value.from array import *arr = array(‘i’, [10, 20, 30])arr.pop(0)for x in arr: print(x, end=’ ‘)Output:20 30
b) Using remove()
The remove() method deletes the first occurrence of a specified value.from array import *arr = array(‘i’, [10, 20, 30])arr.remove(20)for x in arr: print(x, end=’ ‘)Output:10 30
Getting the Length of an Array
The len() method returns the number of elements in an array.
Arrays are a collection of elements of the same data type, allowing efficient storage and manipulation of data.
2. How do you create an array in Python?
Use the array() method from the array module. Example:from array import *arr = array(‘i’, [1, 2, 3])
3. How do you define an array of double values?
Specify the data type ‘d’ for double values:from array import *arr = array(‘d’, [1.1, 2.2, 3.3])Output:1.1 2.2 3.3
Conclusion
Arrays in Python provide a simple yet powerful way to manage and manipulate homogeneous data efficiently. With this guide, you should be well-equipped to utilize arrays in your projects!