Introduction to Python Arrays
In Python, an array is a collection of values that are stored in a contiguous block of memory. Arrays can hold values of the same data type, such as integers or floating-point numbers.
Python arrays are similar to lists in many ways, but they have a few key differences. Unlike lists, arrays are fixed in size and cannot be resized once they are created. This makes arrays more efficient for certain types of operations, such as numerical computations.
Creating Arrays in Python
To create an array in Python, you can use the array
module, which is part of the Python standard library.
Example
Here’s an example of how to create an array of integers:
import array my_array = array.array('i', [1, 2, 3, 4, 5])
In this example, we create an array called my_array using the array() function from the array module. The first argument to the function is the data type of the array, which in this case is ‘i’ for integers. The second argument is a list of integers that we want to store in the array.
Accessing Elements in an Array
You can access individual elements in an array by using their index, just like with lists.
Example
Here’s an example of how to access an element in an array:
import array my_array = array.array('i', [1, 2, 3, 4, 5]) print(my_array[0]) # Output: 1
In this example, we access the first element in the my_array array by using the index 0.
Modifying Elements in an Array
You can modify individual elements in an array by assigning a new value to their index.
Example
Here’s an example of how to modify an element in an array:
import array
my_array = array.array('i', [1, 2, 3, 4, 5]) my_array[0] = 10 print(my_array)
Output
array(‘i’, [10, 2, 3, 4, 5])
In this example, we modify the first element in the my_array array by assigning the value 10 to its index.
Array Methods in Python
Arrays in Python come with several built-in methods that you can use to manipulate them. Here are a few examples:
- append(): Adds a new element to the end of the array.
- insert(): Inserts a new element at a specific position in the array.
- remove(): Removes the first occurrence of a specified element from the array.
- pop(): Removes and returns the element at a specific position in the array.
- reverse(): Reverses the order of the elements in the array.
Example
Here’s an example of how to use the append() method to add a new element to an array:
import array my_array = array.array('i', [1, 2, 3, 4, 5]) my_array.append(6) print(my_array) )
Output
array(‘i’, [1, 2, 3, 4, 5, 6])
In this example, we use the append()
method to add the value 6 to the end of the my_array array.
Method | Description |
---|---|
append() |
Adds a new element to the end of the array. |
extend() |
Adds the elements from another array to the end of the array. |
insert() |
Inserts a new element at a specific position in the array. |
remove() |
Removes the first occurrence of a specified element from the array. |
pop() |
Removes and returns the element at a specific position in the array. |
index() |
Returns the index of the first occurrence of a specified element in the array. |
count() |
Returns the number of occurrences of a specified element in the array. |
reverse() |
Reverses the order of the elements in the array. |
sort() |
Sorts the elements in the array. |