How to declare array in python
How to declare array in python?
Arrays are a great way of storing our numeric values into a variable in continuous locations. For declaring an array in C language, we used to specify the size of the array and then specify the elements it will hold.
The difference between an array and a list is that a list can hold multiple values of different data types whereas an array holds multiple values of the same data type.
In this article, we will discuss how we can declare an array in python.
Arrays and matrices have a special significance in the world of scientific calculations, image processing, and computer vision. The pixels of a picture are represented in the form of matrices for a better understanding of operations on the image.
Arrays can be declared in Python by importing the package numpy.
import numpy as np
The following program illustrates a simple way of declaring an array in python.
import numpy as np #importing the package x=np.array([1,2,3,4]) #array declaration print(x) #printing the array print(type(x)) #type of x print(x.ndim) #dimension of array
On executing the program, we can observe that the output it displays the elements of an array, the data type of our object 'x', and its dimension(it's 1 since it's linear).
A Two-dimensional array can be declared using the following program-
import numpy as np #importing the package x=np.array([[1,2,3,4],[5,6,7,8]]) #array declaration print(x) #printing the array print(type(x)) #type of x print(x.ndim) #dimension of array
print(x) – Displays the array
print(type(x)) – Displays the type of object ‘x’
print(x.ndim) – Displays the dimension which is 2 in this case.
The elements of an array can be accessed using its index-
import numpy as np #importing the package x=np.array([[1,2,3,4],[5,6,7,8]]) #array declaration print(x) #printing the array print(x) print(x) print(x)
The next method that we will use here to declare an array is a range().
Here we will use the range function and for loop for declaring our array.
The first step is to initialize it and then use it for loop and range together to add elements into it.
The following code illustrates how we can implement it-
#array creation arr1= #initialization for i in range(6): arr1.append(i) print(arr1) print(type(arr1))
The next method of array declaration in Python that we will discuss is using the arange() function.
The syntax for arange() is:
np.arange(start = ,stop= ,step= ,dtype= )
start indicates the starting element of our array
stop indicates the last element of our array
step indicates the sequence or common difference between two consecutive elements.
dtype shows the type of elements we want to insert in our array.
Let us understand its working with the help of an example-
import numpy as np arr=np.arange(1,10,1) #array creation print(arr) #displaying the array print(type(arr)) #displaying the type of array
Using Typecodes & Initializers
In this approach of declaring the array we will specify a typecode and initialize the values of our array.
In this program, we will import the module array and use it for array creation-
from array import * #importing array module arr=('i',[1,2,3,4]) #array declaration for i in arr: print(i) #printing the elements print(type(arr))
Type Codes that can be used while specifying an array-
- b-it represents the signed integer of the size 1 byte.
- B-it represents the unsigned integer of the size 1 byte.
- c-it represents the character of the size 1 byte.
- i-it represents the signed integer of the size 2 bytes.
- I-it represents the unsigned integer of the size 2 bytes.
- f-it represents the floating point of the size 4 bytes.
- d-it represents the floating point of the size 8 bytes.
Operations on array in python
The different operations that can be performed on an array in python are-
- Inserting an element in an array that can be done using arr.insert(position,value).
- Deletion operation that can be performed using arr.remove(value).
- Searching an element in an array using arr.index(value).
- Values in an array can be updated using arr[index]=new value.
- If we want to add a number to all the elements of an array we can simply write-
import numpy as np x=np.array([1,2,3,4,5]) print(x+1)
- We can declare two arrays and perform all kind of arithmetic operations on them-
import numpy as np x=np.array([1,2,3,4,5]) y=np.array([6,7,8,9,10]) print(x+y) print(x-y) print(x*2) print(y*4)