Data Structure-Arrays
Table of Content:
What is Array?
An array is a systematic arrangement of similar objects, in rows and columns.
An array is a data structure that contains a group of elements. it stores the elements (values or variables) of same data types.
Why did we need to use Arrays?
Basically, Arrays are used "when there is a need to use many variables of the same type". It can be defined as a sequence of objects which are of the same data type. It is used to store the collection of data,
Arrays are used to implement mathematical vectors and matrices, also used to implement other data structures, such as lists, heap, strings, dictionaries, stacks and queues etc. Historically, it has sometimes been the only way to allocate "dynamic memory", which is changeable.
How arrays can be created?
Array is created in Python by importing array module to the python program. Now, we create and print an array by using Python Language.In Python.So, we can create new data types, called arrays using the NumPy. NumPy is basically library of Python. NumPy arrays are used for numerical analyses and contain only a single data type.
First, import NumPy Library and then use array() function to create an array. The array() function takes a list as an input.
Recommended by LinkedIn
The below code creates an array named array. Now, we create and print an array by using Python Language.
First, import NumPy Library and then use array() function to create an array. The array() function takes a list as an input. The below code creates an array named array.
import numpy as np
My_Array = numpy.array([0, 1, 2, 3, 4])
print(My_Array)
Output:
[0, 1, 2, 3, 4]
Further you add two NumPy arrays. The result is sum of both the arrays, element wise.
import numpy as np
Array_1 = np.array([1, 2, 3])
Array_2 = np.array([4, 5, 6])
print(Array_1 + Array_2)
Output:
[5 7 9]
Example of Array indexing:
Although, you can easily access the elements by index in array, using indexing notation.
import numpy as np
Array = np.array(['Jan', 'Feb', 'March', 'Apr', 'May'])
print(Array[3]) # it will pick the element of 3rd index
Output:
Apr
We can also slice the range of the elements by Slicing method. In this method, we provide a two ranges, the starting range number is include and second range number is exclude.
Array = array[2:5] # it will pick the elements from 2 to 4 indexe
print(Array)
Output:
['March', 'Apr', 'May']
Continue.....
Informative 😍