python arrays

Python Arrays – Learn Python with me

Welcome to our blog and our new chapter in the Python tutorial series, Python Arrays. In this post, we will discuss the concept of Python arrays in detail.

Why do we need Python Arrays?

Let us suppose you want to write a program that will list the salaries of each team member in a company. If the company you are talking about has only three employees, you can declare each one of them as a variable with their corresponding names. What if there are 50 or more than 50 employees? You cannot keep declaring the variables for 50 employees because it means you literally have to type 50 lines of code.

Imagine you want to do the same for a list of 150 employees. In these cases, you can declare an array and store all the salary values there. An array at the end of the day helps programmers by decreasing the size of the program. Let us discover more about the arrays in Python now.

Python Arrays

An array is an object that stores a group of elements of a similar datatype. One of the main benefits of the array is that you can save and process a combination of elements efficiently. Before you start using the arrays, you need to keep two essential things in mind mentioned below.

  • An array can contain only similar kind of data. It means that if you store the float elements into an array, you cannot add integer values to the same array
  • The size of an array decreases or increases dynamically and you don’t have to declare its size. As you keep adding or removing the elements, its corresponding size will be increased or decreased automatically.

Python has a standard module with the name array using which you can create and use arrays in Python programs. If you want to know more about arrays, you can type help() in the Python console, hit enter, type arrays and hit enter again.

Benefits of Arrays

  • Using the array module, you can use certain methods to process the elements of any array in your program.
  • They are easy to be worked with.
  • The arrays have the ability to increase or decrease its size dynamically during runtime.
  • Since the arrays can increase its size dynamically, its size is not fixed.
  • Arrays are also like lists with the major difference being the former can store only one kind of data and the latter can store any kind of data.

How to create an Array?

We can create an array using the format shared below. As mentioned earlier, arrays hold only items or elements of a similar kind.

arrayname = array(typecode, [elements])

The typecode in the format shared above is different for each kind of type of data. Please refer to the table for more information on the same.

TypecodeDescription
uUnicode character
ddouble-precision floating-point
ffloating point
Lunsigned integer
lsigned integer
Iunsigned integer
isigned integer
Bunsigned integer
bsigned integer

Let us now look at an example of how to create an array.

import array as arr #import array with alias arr
array1=arr.array('i',[2,3,5,7]) #creating an array and storing in array1 variable

You can see in the above program that to use the arrays in the program, we had to import the array module in the first line. Additionally, we also added an alias arr for the array module for easy reference in the program. You can use your aliases for the references in your program. In the second line of code, we referred to the array class of the array module using the arr.array (This really refers to the array.array class). We then added the new array with elements to the variable array1.

Let us look at an example to add elements to the array and print them using a “for loop”.

import array as arr
array1=arr.array('i',[2,3,5,7])
print("The elements in the array are: ")
for i in array1:
    print(i)

Output

The elements in the array are: 
2
3
5
7

We can also create an array by importing the array module in the first line of the program, as “from array import *”. This syntax will be helpful in cases when you don’t want to use an alias. Let us look at an example below to understand the same.

#program to create an array and print the elements
from array import *
array1=array('i',[2,3,5,7])

print("The elements in the array are printed below: ")
for i in array1:
    print(i)

Output

The elements in the array are printed below: 
2
3
5
7

Slicing and Indexing on Arrays

Just as the name sounds, indexing refers to the location number of the element in an array. Let us look at the example below to understand the same.

Python Array Arrangement in Memory

You can see in the image above that the indexing of the elements begins from zero, and the first element of the array, 11, is positioned at the zeroeth index and the remaining elements are placed at the following index positions. Let us now look at an example to print the index positions of each element in an array.

#program to create an array and print the elements and index positions
from array import *
array1=array('i',[2,3,5,7])

length1=len(array1) #finds the number of elements in array
print("The number of elements in the declared array is: ",length1)
print("The elements and index positions in the array are printed below:")
for a in range(length1):
    print("array1 [{0}] = {1}".format(a,array1[a]))

Output

The number of elements in the declared array is:  4
The elements and index positions in the array are printed below:
array1 [0] = 2
array1 [1] = 3
array1 [2] = 5
array1 [3] = 7

A slice refers to the part of an array. You can use slicing to retrieve only a section of elements from an array. Let us now look at an example program to understand the concept of slicing in Python.

#program to create an array and print the elements
from array import *
array1=array('i',[2,3,5,7])
b=array1[0:2] #stores into the array 'b'the numbers from index position 0 till 2
print(b) #prints the array b
p=array1[0:] #stores into the array p the numbers from index position 0 till end
print(p)
q=array1[-2:] #stores into array q the last two numbers of the array1
print(q)

Output

array('i', [2, 3])
array('i', [2, 3, 5, 7])
array('i', [5, 7])

Processing the arrays using methods

The Array class contains many methods in the array module in Python, using which you can perform specific operations efficiently. Please refer to the Array Methods in the table below for your reference.

Method nameDescription of the method
tostring()converts the array into a string
tolist()converts the array into a list
tofile()Writes all elements into a file
reverse()reverse the elements’ order in an array
remove()eliminates the first occurrence of an element in an array
pop()remove the last element of the array
pop(element)removes the item, ‘element’ from the array and returns it
insert(a,b)inserts ‘a’ in the position ‘b’ in the array
index(a)provides the index number of the first occurrence of the element ‘a’ in the array
a.fromstring(b)Appends item from the string ‘b’ to the end of the array ‘a’
p.fromlist(listq)Appends item from the list, ‘listq’ to the end of the array
p.fromfile(a,b)Reads b items (in binary format) from the file object ‘a’ and appends to the end of the array ‘p’. In this case, ‘a’ must be a file
p.extend(q)Appends q at the end of the array p
p.count(q)Returns the number of occurrences of q in the array p
p.append(q)Adds an element p at the end of the existing array p

Besides the methods, the array class also contains the variables as mentioned in the table below.

Variable nameDescription of the variable
itemsizeRefers to the size of items in the array
typecodeRefers to the type code character used to create the array.

Let us now look at a program to understand the concept of methods in arrays:

#program to understand the concept of methods in array
from array import *

#construct an array with integer values
int1=array('i',[11,22,33,44,55])
print("The declared array is shared below: ")
print(int1)

#append 33 and 66 to the existing array
int1.append(33)
int1.append(66)
print("The array after appending 33 and 66: ")
print(int1)

#insert 70 at the position number 1 in int1 array
int1.insert(1,70)
print("The array after inserting 70 at the position 1: ")
print(int1)

#remove an element from the array int1
int1.remove(22)
print("The array after removing 22: ")
print(int1)

#remove last element using the pop() method
a=int1.pop()
print("Array after using the pop(): ")
print(int1)
print("The popped element is: ",a)

#to find the position of the element using index() method
b=int1.index(33)
print("The element 33 appears first at the position: ",b)

#converts an array into list using tolist() method
list1=int1.tolist()
print("The list is: ")
print(list1)
print("The array is: ")
print(int1)

Output

The declared array is shared below: 
array('i', [11, 22, 33, 44, 55])
The array after appending 33 and 66: 
array('i', [11, 22, 33, 44, 55, 33, 66])
The array after inserting 70 at the position 1: 
array('i', [11, 70, 22, 33, 44, 55, 33, 66])
The array after removing 22: 
array('i', [11, 70, 33, 44, 55, 33, 66])
Array after using the pop(): 
array('i', [11, 70, 33, 44, 55, 33])
The popped element is:  66
The element 33 appears first at the position:  2
The list is: 
[11, 70, 33, 44, 55, 33]
The array is: 
array('i', [11, 70, 33, 44, 55, 33])

Types of Arrays

All major programming languages like Java or C offers two kinds of arrays. They are:

  1. Single Dimensional Arrays
  2. Multi-dimensional Arrays

We have used only single dimensional arrays so far in the examples above. Unlike other programming languages, Python does not support multi-dimensional arrays. However, as mentioned in our earlier chapters, you can rely on additional packages like numpy to add multi-dimensional arrays in Python. If you want to know only about Multi-Dimensional arrays, please visit the next chapter for the same. We will now have a look at how to work with Single-Dimensional arrays using numpy.

How to work with Arrays using numpy

numpy has several packages containing several variables, functions, classes, etc., to work with scientific calculations in Python. You can use numpy to work with both single and multi-dimensional arrays in Python. If you want to use numpy in your Python program, you must import the numpy module as mentioned below.

import numpy

Let us look at an example to create a simple array

#program to create single dimensional array
import numpy as num
array1=num.array([11,22,33,44,55]) #create array1
print(array1) #display the array

Output

[11 22 33 44 55]

You can see in the above example that we imported the package numpy with an alias ‘num’. That’s why we had to mention the alias name to use the array method in the subsequent lines of code. If you have so many lines of code, it will be annoying to mention the alias name every time. However, you can overcome this annoyance by importing the package numpy as explained below.

from numpy import *

Let us re-write the same program once again by importing the numpy module more quickly by eliminating the alias.

#program to create single dimensional array
from numpy import *
array1=array([11,22,33,44,55]) #create array1
print(array1) #display the array

Output

[11 22 33 44 55]

How to create arrays using the array() function of the numpy module?

Using the array() function in the numpy module, we can create an array. We can mention the datatype of the elements either as ‘float’ or ‘int’. Let us look at some examples below.

from numpy import *
array1=array([11,22,33,44,55],int) #declares int type array
array2=array([3.7,7.3,-2.5],float) #declares float type array

You can see in the examples above that we declared the int type and float type arrays by specifying the datatype. However, the Python interpreter will also automatically detect the datatypes based on the elements entered in the array. Let us look at an example to enter character elements into an array as mentioned below.

#program to create array with characters
from numpy import *
array1=array(['a','e','i','o','u'])
print(array1)

Output

['a' 'e' 'i' 'o' 'u']

How to create arrays using the linspace() function?

We use the linspace() function to construct an array with evenly spaced points between the beginning and ending points of the array. Please refer to the format of linspace() below.

linspace(start, stop, n)

‘start’ refers to the first element, and ‘stop’ refers to the last element. ‘n’ refers to the number of parts an element should be divided. If ‘n’ is not specified, then it is considered as 50. Let us now look at an example below to understand the concept of linspace() function.

#program to understand the concept of linspace() function
from numpy import *
#divide 0 to 25 into 5 parts and enter those points into array
a=linspace(0,25,5)
print(a)

Output

[ 0.    6.25 12.5  18.75 25.  ]

In the above program, you can notice that we created an array ‘a’ with the first element as 0 and the last element as 25. The range of the first and final elements are equally divided into five parts, and that’s why the output is printed as 0, 6.25, 12.5, 18.75, and 25. All thanks to the linspace() function for the same.

How to create arrays using the logspace() function?

The logspace() function is also like linspace() function. While the linspace() function makes the equally spaced points, the logspace() function also makes similar equally spaced points on a logarithmically spaced scale. Please refer to the format of the logspace() function below.

logspace(start, stop, n)

The logspace() function begins at the value 10 to the power of ‘start’ and ends at the value 10 to the power of ‘stop’. Like the linspace() function, if the ‘n’ is not mentioned, it is considered 50. Let us now look at an example program.

#program to understand the concept of logspace() function
from numpy import *

#divide the range: 10 power 1 to 10 power 5 into 5 equal parts
#and store those points in the array
a=logspace(1,5,5)

#number of elements in a
number1=len(a)
print("The number of elements: {0}".format(number1))
print(a)

Output

The number of elements: 5
[1.e+01 1.e+02 1.e+03 1.e+04 1.e+05]

How to create arrays using the arange() function?

The arange() function in numpy works much like the range() function in Python. Please refer to the arange() function below.

arange(start, stop, stepsize)

Let us now look at an example program using the arange() function.

#program to understand the concept of arange() function
from numpy import *
a=arange(5)
#creates an array 'a' with elements from 0 to 4
print("The array a: ",a)
b=arange(3,7)
#creates an array 'b' with elements from 3 to 6
print("The array b: ",b)
c=arange(3,15,2)
#creates an array 'c' with elements from 3 to 15
#with stepsize of 2
print("The array c: ",c)

Output

The array a:  [0 1 2 3 4]
The array b:  [3 4 5 6]
The array c:  [ 3  5  7  9 11 13]

How to create arrays using the zeros() and ones() functions?

We can use the ones() and zeros() functions to create an array with all 1s and 0s. Please refer to the format below.

zeros(n, datatype)
ones(n, datatype)

Here, the ‘n’ refers to the number of elements in the array, and the datatype refers to the datatype that we will use in the array. Let us now look at an example program below.

#program to understand the concept of zeros() and ones() functions
from numpy import *
p=zeros(7,int)
print("The array p: ",p)
q=ones(3,float)
print("The array q: ",q)
r=ones(7)
print("The array r: ",r)

Output

The array p:  [0 0 0 0 0 0 0]
The array q:  [1. 1. 1.]
The array r:  [1. 1. 1. 1. 1. 1. 1.]

Mathematical Operations on Arrays

We can perform several mathematical operations such as division, subtraction, multiplication, addition, etc., on the elements in the array. Besides that, the functions that are available in the math module can also be implemented on the elements of the array. Let us look at an example below to understand this concept.

#program to understand mathematical operations on arrays
from numpy import *
array1=array([11,22,33,44,55],int)
#creates an array with name array1
array2=array([1,2,3,4,5],int)
#creates an array with name array1
array3=array1-array2
#subtracts array2 from array1 and stores in array3
print("The subtracted result of array 2 from array 1: ")
print(array3) #prints the subtracted result
array4=array3+3
#adds 3 to each element of array3 and stores in array4
print("The array4 is: ")
print(array4)

Output

The subtracted result of array 2 from array 1: 
[10 20 30 40 50]
The array4 is: 
[13 23 33 43 53]

You can see in the program above that we created two arrays with the names array1 and array2. We added a new array with the name, array3 and stored in it the subtracted result of “array2-array1”. After this, using array3+3, we added each element in the array3 with 3 and stored the result in a new array, array4. Similarly, you can perform other mathematical operations as well on the arrays. This is called vectorized operations as the complete array (or vector) is processed more like a variable.

Advantages of vectorized operations

  1. They are faster. You can perform mathematical operations on the arrays very easily without having to rely on the looping concepts.
  2. They are clear syntatically and that’s why you will be able to understand the logic easily.
  3. Vectorized operations in Python provides only lesser code.

Please also be informed that you can perform other complex mathematical functions such as abs(), exp(), sqrt(), sin(), and cos() etc. on the element of an array. They are all available in the numpy module. Please refer to the table below that lists the functions that we can process on the numpy arrays.

Function NameDescription of the function
sin(array1)Calculates the sine value of each element in the array ‘array1’.
cos(array1) Calculates the cosine value of each element in the array ‘array1’.
tan(array1) Calculates the tangent value of each element in the array ‘array1’.
arcsin(array1) Calculates the sine inverse value of each element in the array ‘array1’.
arcos(array1) Calculates the cosine inverse value of each element in the array ‘array1’.
arctan(array1) Calculates the tangent inverse value of each element in the array ‘array1’.
log(array1)Calculates the natural logarithmic value of each element in the array ‘array1’.
abs(array1) Calculates the absolute value of each element in the array ‘array1’.
sqrt(array1) Calculates the square root value of each element in the array ‘array1’.
power(array1, n)Returns the power value of each element in the array ‘array1’ when raised to the power of ‘n’.
exp(array1) Calculates the exponential value of each element in the array ‘array1’.
sum(array1)Returns the sum of all the elements in the array ‘array1’.
prod(array1)Returns the product of all the elements in the array ‘array1’.
min(array1)Returns the smallest element in the array ‘array1’.
max(array1)Returns the biggest element in the array ‘array1’.
mean(array1)Returns the mean value or the average value of all the elements in the array ‘array1’.
median(array1)Returns the median value of the elements in the array ‘array1’.
var(array1)Returns the variance of all the elements in the array ‘array1’.
cov(array1)Returns the covariance of all the elements in the array ‘array1’.
std(array1)Provides the standard deviation of all the elements in the array ‘array1’.
argmin(array1)Provides the index of smallest element in the array ‘array1’. The count begins from 0.
argmax(array1)Provides the biggest element’s index in the array ‘array1’. The count begins from 0.
unique(array1)Provides an array that has unique elements of the array ‘array1’.
sort(array1)Provides an array with sorted elements of the array ‘array1’ in ascending order.
concatenate(array1,array2)Returns an array after joining array1 and array2 arrays. You should enter the elements of array1 and array2 as elements of list.

Let us now look at an example to witness some mathematical operations on the numpy arrays.

#program to see mathematical operations on numpy array
from numpy import *

#create an array with name array1
array1=array([11,22,33,44,55])
print("The declared array: ")
print(array1)

#arithmetic operations on the array
print("When you add 3: ",array1+3)
print("When you subtract 3: ",array1-3)
print("When you divide by 3: ",array1/3)
print("When you multiply by 3: ",array1*3)
print("After modulus with 3: ",array1%3)

#example of expressions in array
print("The expression value: ",(array1+3)**3-7)

#math functions on array1
print("Tan value of elements in array1: ",tan(array1))
print("Sine value of elements in array1: ",sin(array1))
print("Cosine value of elements in array1: ",cos(array1))
print("Smallest Element in the array1: ",min(array1))
print("Biggest element in the array1: ",max(array1))
print("Sum of all the elements in the array1",sum(array1))
print("Average value of all the elements in array1: ",mean(array1))

Output

The declared array: 
[11 22 33 44 55]
When you add 3:  [14 25 36 47 58]
When you subtract 3:  [ 8 19 30 41 52]
When you divide by 3:  [ 3.66666667  7.33333333 11.         14.66666667 18.33333333]
When you multiply by 3:  [ 33  66  99 132 165]
After modulus with 3:  [2 1 0 2 1]
The expression value:  [  2737  15618  46649 103816 195105]
Tan value of elements in array1:  [-2.25950846e+02  8.85165604e-03 -7.53130148e+01  1.77046993e-02
 -4.51830879e+01]
Sine value of elements in array1:  [-0.99999021 -0.00885131  0.99991186  0.01770193 -0.99975517]
Cosine value of elements in array1:  [ 0.0044257  -0.99996083 -0.01327675  0.99984331  0.02212676]
Smallest Element in the array1:  11
Biggest element in the array1:  55
Sum of all the elements in the array1 165
Average value of all the elements in array1:  33.0

Compare arrays using relational operators

We can compare the arrays using the relational operators such as >, <, >=, <=, !=, and ==. Boolean type values such as True or False are returned after the elements in the array are compared using these relational operators. Let us look at an example now.

#compare the arrays and display the boolean result
from numpy import *
array1=array([10,20,30,40,50])
array2=array([1,2,3,4,5])
print("Array1 < Array2? : ",array1<array2)
print("Array1 > Array? : ",array1>array2)
print("Array1 == Array2? : ",array1==array2)

Output

Array1 < Array2? :  [False False False False False]
Array1 > Array? :  [ True  True  True  True  True]
Array1 == Array2? :  [False False False False False]

You can see that each element on both the declared arrays are compared, and their corresponding results are displayed in Boolean values of True or False, as mentioned in the program above.

How to create an alias to an array?

If ‘p’ is an array, we can assign it to ‘q’ as mentioned below.

q=p

The assignment statement mentioned above is a straightforward one. If ‘p’ is an array, we assigned it to ‘q’. This assignment statement means that ‘q’ is not a new array, and therefore no memory is allocated to ‘q’. It means that if you modify any element in the array ‘q’, the same gets reflected on the array ‘p’ as well. This concept is called ‘aliasing’.

Aliasing in Arrays

Let us look at a program to understand the concept of aliasing arrays in Python.

#program to understand the concept of aliasing an array
from numpy import *
a=arange(3,7) #creates elements from 3 to 6
b=a #gives another name b to a
print("Declared array: ",a)
print("Alias array: ",b)
b[0]=99 #modifies the first element of b array
print("After modification: ")
print("Declared Array: ",a)
print("Alias array: ",b)

Output

Declared array:  [3 4 5 6]
Alias array:  [3 4 5 6]
After modification: 
Declared Array:  [99  4  5  6]
Alias array:  [99  4  5  6]

You can see in the program above that both arrays displayed the same result when we tried changing an element in one of the arrays.

How to create another separate independent array as same as the existing array?

We have discussed above in the aliasing of arrays that if we create an alias for an existing array, any changes on the elements in one of the arrays will display the same result on both the arrays. However, you might come across situations where you want to create an independent copy of an existing array so that if you update any one of the arrays, those changes will be confined to only the array in which the changes are updated. You can achieve this requirement by using the copy() method. However, let us look at the view() method first.

Viewing in Arrays

In the figure shared above, you can see that the elements in both p and q are dependent on each other when we use the view() method. That’s why the update on any one of the elements will also update the element at the same position on the other array. Let us look at an example now.

#program to understand the concept of viewing an array
from numpy import *

p=arange(2,7) #creates an array with elements 2 to 6
q=p.view() #creates a view of p and name it q
print("Declared array p: ",p)
print("New array q: ",q)
q[0]=99
print("After modifying: ")
print("Declared array without changes: ",p)
print("New array with update to the first element: ",q)

Output

Declared array p:  [2 3 4 5 6]
New array q:  [2 3 4 5 6]
After modifying: 
Declared array without changes:  [99  3  4  5  6]
New array with update to the first element:  [99  3  4  5  6]

Using the view() method, we successfully created another array similar to the existing one. Updating the first element in one of the arrays will update the value at the same position in another array. But if you want both arrays to stay independent from each other, then you have to use the copy() method.

The copy() method allows you to create an entire copy of an existing array. If you create a new array by using the copy() method on a current array, it will not alter the existing array. Please refer to the figure below for more details.

Copying in Arrays

Let us now look at an example to understand the concept of the copy() method on arrays in Python.

#program to understand the concept of copy() method on an array
from numpy import *

p=arange(2,7) #creates an array with elements 2 to 6
q=p.copy() #creates a copy of p and name it q
print("Declared array p: ",p)
print("New array q: ",q)
q[0]=99
print("After modifying: ")
print("Declared array without changes: ",p)
print("New array with update to the first element: ",q)

Output

Declared array p:  [2 3 4 5 6]
New array q:  [2 3 4 5 6]
After modifying: 
Declared array without changes:  [2 3 4 5 6]
New array with update to the first element:  [99  3  4  5  6]

You can see in the program above that we created a new array copy with the name ‘q’ for an existing array called ‘p’. However, when we updated the first element of the array ‘q’, it didn’t change the array ‘p’. It means that both the arrays ‘p’ and ‘q’ are independent of one another.

Indexing and Slicing in NumPy arrays

Indexing represents the positions of the elements. We discussed the concept of indexing in our earlier chapters. So, let us now look at an example program to understand how we can achieve indexing in Python using the numpy module.

#indexing and slicing in arrays using numpy
from numpy import *
arr=arange(3,8) #create an array with range from 3 to 7
print(arr)
for i,j in enumerate(arr):
    print('{0} is stored at index position {1}'.format(j,i))
arr=arr[1:5:2]
#first element till last element with step size of 2
print(arr)
for i,j in enumerate(arr):
    print('{0} is stored at index position {1}'.format(j,i))

Output

[3 4 5 6 7]
3 is stored at index position 0
4 is stored at index position 1
5 is stored at index position 2
6 is stored at index position 3
7 is stored at index position 4
[4 6]
4 is stored at index position 0
6 is stored at index position 1

In the above program, we sliced the positions of the main array [3 4 5 6 7] into [ 4 6 ] using the code arr=arr[1:5:2]. This is an example of slicing and it follows the below format.

nameofarray[start:stop:stepsize]

We also used a for loop to print the index positions of each element before and after slicing. Let us now look at one more example to understand slicing.

#slicing an array with example
from numpy import *
arr=arange(3,9)
#create an array with elements from 3 to 8
print("Main array: ",arr) #print the array
for i,j in enumerate(arr):
    print("{0} number is at index position {1}".format(j,i))
print("Let's do some slicing")
arr1=arr[1:6:2]
#retrieve 1st element to one element prior to 6th element
print("arr1 array: ",arr1) #prints the new arr1 array
for i,j in enumerate(arr1):
    print("{0} number is at index position {1}".format(j,i))
arr2=arr[::] #retrieve all elements from arr array
print("arr2 array: ",arr2)#prints the arr2 array
for i,j in enumerate(arr2):
    print("{0} number is at index position {1}".format(j,i))
arr3=arr[-2:2:-1]
#retrieve 2nd element from last and its subsequent number
#in decreasing order due to step size -1
print("arr3 array: ",arr3)#prints the arr3 array
for i,j in enumerate(arr3):
    print("{0} number is at the index position {1}".format(j,i))
arr4=arr[:-2:]
#retrieves first element till the second number from last
print("arr4 array: ",arr4) #prints the array arr4
for i,j in enumerate(arr4):
    print("{0} number is at the index position {1}".format(j,i))
print("The End")

Output

Main array:  [3 4 5 6 7 8]
3 number is at index position 0
4 number is at index position 1
5 number is at index position 2
6 number is at index position 3
7 number is at index position 4
8 number is at index position 5
Let's do some slicing
arr1 array:  [4 6 8]
4 number is at index position 0
6 number is at index position 1
8 number is at index position 2
arr2 array:  [3 4 5 6 7 8]
3 number is at index position 0
4 number is at index position 1
5 number is at index position 2
6 number is at index position 3
7 number is at index position 4
8 number is at index position 5
arr3 array:  [7 6]
7 number is at the index position 0
6 number is at the index position 1
arr4 array:  [3 4 5 6]
3 number is at the index position 0
4 number is at the index position 1
5 number is at the index position 2
6 number is at the index position 3
The End

We will cover the Multi Dimensional Arrays in detail in our next post. Please feel free to add your views/queries in the comments section below.

Scroll to Top