How to declare an array in Python
Understanding Arrays
An array can be thought of as a container that holds a fixed number of items, where each item is of the same type. In the context of programming, it's like a straight line of boxes where each box can hold a specific type of item. For example, if you have a box that can only hold apples, then you can say that this is an array of apples.
Arrays are particularly useful when you want to store multiple items of the same type. Instead of creating separate variables for each item, you can store them all in one array.
Declaring an Array in Python
In Python, arrays are not used as frequently as lists. However, they still serve a purpose when dealing with large datasets. Declaring an array in Python is quite simple. We need to import the array
module before we can create an array. Here's an example:
import array as arr
a = arr.array('d', [1.1, 2.2, 3.3])
The first argument ('d') is a type code. This determines the type of the array during creation. 'd' is the type code for double precision floating point. The second argument is an iterable, this could be a list or a tuple.
Understanding the Type Codes
The type code is a character that specifies the type of values the array will hold. Here are some common type codes:
- 'b' : Represents signed integer of size 1 byte
- 'B' : Represents unsigned integer of size 1 byte
- 'c' : Represents character of size 1 byte
- 'u' : Represents unicode character of size 2 bytes
- 'h' : Represents signed integer of size 2 bytes
- 'H' : Represents unsigned integer of size 2 bytes
- 'i' : Represents signed integer of size 2 bytes
- 'I' : Represents unsigned integer of size 2 bytes
- 'f' : Represents floating point of size 4 bytes
- 'd' : Represents floating point of size 8 bytes
Adding Elements to an Array
Adding elements to an array is like adding more boxes to our line of boxes. Python provides us with the append()
method to add a single element to the array. For adding multiple elements, we use the extend()
method.
a.append(4.4)
a.extend([5.5, 6.6, 7.7])
Accessing Elements in an Array
Accessing elements in an array is like opening the box you want and taking out the item. In Python, you can access the elements of an array using their index, just like you would do with a list. The index starts from 0, so the first element of the array is at index 0.
print(a[0]) # Prints 1.1
print(a[2]) # Prints 3.3
Removing Elements from an Array
Removing elements from an array is like removing boxes from our line of boxes. Python gives us the remove()
method to remove the specified item, and the pop()
method to remove an item at a specified position.
a.remove(1.1)
print(a) # Prints array('d', [2.2, 3.3, 4.4, 5.5, 6.6, 7.7])
b = a.pop(2)
print(a) # Prints array('d', [2.2, 3.3, 5.5, 6.6, 7.7])
print(b) # Prints 4.4
Conclusion
Learning to declare an array in Python is like learning to juggle. At first, it might seem daunting keeping track of all these items and their positions. But with practice, it can become second nature. You'll find that arrays, like juggling balls, follow certain rules and patterns. Once you understand these, you can do more than just keep them in the air. You can start to create your own patterns, solve complicated problems, and even put on a show for others. So, keep practicing, keep exploring, and most importantly, keep coding!