An array data structure belongs to the “must-import” category. To use an array in Python, you’ll need to import this data structure from the NumPy package or the array module.
Python has built-in data structures like lists, tuples, sets, and dictionaries for which there is no need to include any package or module.
An array is a data structure that stores a collection of items. Like lists, arrays are ordered, mutable, enclosed in square brackets, and able to store non-unique items.
But when it comes to the array’s ability to store different data types, the answer is not as straightforward. It depends on the kind of array used.
To use arrays in Python, you need to import either an array module or a NumPy package.
The Python array module requires all array elements to be of the same type. Moreover, to create an array, you’ll need to specify a value type. In the code below, the “i” signifies that all elements in array_1 are integers:
import array as arr array_1 = arr.array("i", [3, 6, 9, 12]) print(array_1) print(type(array_1))
On the other hand, NumPy arrays support different data types. To create a NumPy array, you only need to specify the items (enclosed in square brackets, of course):
import numpy as np array_2 = np.array(["numbers", 3, 6, 9, 12]) print (array_2) print(type(array_2))
As you can see, array_2 contains one item of the string type (i.e., “numbers”) and four integers.
array = np.array([3, 6, 9, 12]) division = array/3 print(division) print (type(division))
list = [3, 6, 9, 12] division = list/3
Of course, it’s possible to do a mathematical operation with a list, but it’s much less efficient
If you like dEexams.com and would like to contribute, you can write your article here or mail your article to firstname.lastname@example.org . See your article appearing on the dEexams.com main page and help others to learn.