Numpy Introduction
What is numpy?
Numpy is a Python library used for working with arrays.
It also has functions for working in the domain of linear algebra, Fourier transform, and matrices.
NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.
NumPy stands for Numerical Python.
Why Use NumPy?
In Python, we have lists that serve the purpose of arrays, but they are slow to process.
NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are very important.
Data Science: is a branch of computer science where we study how to store, use and analyze data for deriving information from it.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory, unlike lists, so processes can access and manipulate them very efficiently.
This behavior is called locality of reference in computer science.
This is the main reason why NumPy is faster than lists. Also, it is optimized to work with the latest CPU architectures.
Which Language is NumPy written in?
NumPy is a Python library and is written partially in Python, but most of the parts that require fast computation are written in C or C++.
Installation of NumPy
If you have Python and PIP already installed on a system, then installation of NumPy is very easy.
Install it using this command:
C:\Users\Your Name>pip install numpy
If this command fails, then use a Python distribution that already has NumPy installed like Anaconda, Spyder, etc.
Import NumPy
Once NumPy is installed, import it in your applications by adding the import keyword:
import numpy
Now Numpy is imported and ready to use.
Example
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing:
import numpy as np
Now the NumPy package can be referred to as np instead of numpy.
Example
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Checking NumPy Version
The version string is stored under the __version__ attribute.
Example
import numpy as np
print(np.__version__)
NumPy Creating Arrays
Create a NumPy ndarray Object
NumPy is used to work with arrays. The array object in NumPy is called ndarray.
We can create a Nuarrayrray object by using the array() function.
Example
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
type(): This built-in Python function tells us the type of the object passed to it. Like in above code it shows that arr is numpy. ndarrayype.
To create an array, we can pass a list, tuple, or any array-like object into the array() method, and it will be converted into an array:
Example
Use a tuple to create a NumPy array:
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
nested array: arrays that have arrays as their elements.
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Example
Create a 0-D array with value 42
of
import numpy as np
arr = np.array(42)
print(arr)
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
These are the most common and basic arrays.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
These are often used to represent matrix or 2nd order tensors.
NumPy has a whole sub-module dedicated towards matrix operations called numpy. mat
Example
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called a 3-D array.
These are often used to represent a 3rd order tensor.
Example
Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
arr = n p.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
Check the Number of Dimensions.?
NumPy Arrays provides the dim attribute that returns an integer that tells us how many dimensions the array have.
Example
Check how many dimensions the arrays have:
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
Higher Dimensional Arrays
An array can have any number of dimensions.
When the array is created, you can define the number of dimensions by using the nadminargument.
Example
Create an array with 5 dimensions and verify that it has 5 dimensions:
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)
In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector, the 3rd dim has 1 element that is the matrix with the vector, the 2nd dim has 1 element that is a 3D array and 1st dim has 1 element that is a 4D array.
NumPy Array Indexing
Access Array Elements
Array indexing is the same as accessing an array element.
You can access an array element by referring to its index number.
The indexes in NumPy arrays start with 0, meaning that the first element has an indof ex 0, and the second han as of index 1 etc.
Example
Get the first element from the following array:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])
Example
Get the second element from the following array.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[1])
Example
Get the third and fourth elements from the following array and add them.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])
Access 2-D Arrays
To access elements from 2-D arrays we can use comma-separated integers representing the dimension and the index of the element.
Example
Access the 2nd element on 1st dim:
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st dim: ', arr[0, 1])
Access the 5th element on 2nd dim:
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('5th element on 2nd dim: ', arr[1, 4])
Access 3-D Arrays
To access elements from 3-D arrays we can use comma-separated integers representing the dimensions and the index of the element.
Example
Access the third element of the second array of the first array:
import numpy as np
arr = np. array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
Example Explained
arr[0, 1, 2] prints the value 6.
And this is why:
The first number represents the first dimension, which contains two arrays:
[[1, 2, 3], [4, 5, 6]]
and:
[[7, 8, 9], [10, 11, 12]]
Since we selected 0, we are left with the first array:
[[1, 2, 3], [4, 5, 6]]
The second number represents the second dimension, which also contains two arrays:
[1, 2, 3]
and:
[4, 5, 6]
Since we selected 1, we are left with the second array:
[4, 5, 6]
The third number represents the third dimension, which contains three values:
4
5
6
Since we selected 2, we end up with the third value:
6
Negative Indexing
Use negative indexing to access an array from the end.
Example
Print the last element from the 2nd dim:
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('Last element from 2nd dim: ', arr[1, -1])
Related Blogs
Sep 08, 2026
296 Views
Aug 31, 2026
420 Views
Jun 27, 2026
430 Views
Jun 24, 2026
342 Views
Jun 23, 2026
350 Views
Jun 10, 2026
328 Views
Jun 09, 2026
409 Views
Jun 08, 2026
323 Views
May 28, 2026
411 Views
May 14, 2026
491 Views
May 02, 2026
433 Views
Apr 26, 2026
584 Views
Apr 21, 2026
1052 Views
Apr 19, 2026
392 Views
Apr 15, 2026
397 Views
Mar 23, 2026
1626 Views
Mar 16, 2026
1940 Views
Mar 09, 2026
764 Views
Mar 02, 2026
584 Views
Feb 25, 2026
1064 Views
Feb 18, 2026
1726 Views
Feb 09, 2026
673 Views
Jan 31, 2026
883 Views
Jan 10, 2026
7047 Views
Dec 30, 2025
897 Views
Dec 20, 2025
875 Views
Dec 04, 2024
2500 Views
Nov 29, 2024
2060 Views
Nov 25, 2024
1810 Views
Nov 18, 2024
4015 Views
Nov 13, 2024
2667 Views
Nov 11, 2024
2105 Views
Nov 06, 2024
2592 Views
Nov 04, 2024
1942 Views
Oct 30, 2024
2616 Views
Oct 28, 2024
2566 Views
Oct 23, 2024
3590 Views
Oct 21, 2024
2109 Views
Oct 16, 2024
2907 Views
Oct 14, 2024
3493 Views
Oct 09, 2024
2012 Views
Oct 07, 2024
2199 Views
Oct 04, 2024
3125 Views
Sep 30, 2024
4902 Views
Sep 23, 2024
1959 Views
Sep 20, 2024
3318 Views
Aug 02, 2024
2200 Views
Jun 08, 2023
1743 Views
Jun 01, 2023
1875 Views
May 19, 2023
4076 Views
Jul 05, 2022
5051 Views
May 08, 2022
9408 Views
Apr 17, 2022
29557 Views
Dec 02, 2021
5581 Views
Sep 13, 2021
4952 Views
May 29, 2021
5543 Views
Apr 27, 2021
3784 Views
Apr 06, 2021
5817 Views
Apr 03, 2021
4364 Views
Mar 26, 2021
2885 Views
Mar 19, 2021
3775 Views
Mar 19, 2021
3151 Views
Mar 05, 2021
2986 Views
Feb 18, 2021
4153 Views
Feb 18, 2021
3487 Views
Feb 11, 2021
2978 Views
Nov 10, 2020
3268 Views
Sep 09, 2020
4610 Views
Aug 24, 2020
4954 Views
Aug 20, 2020
4590 Views
Aug 18, 2020
6244 Views
Jul 10, 2020
8403 Views
Jul 07, 2020
12078 Views
Jul 07, 2020
10331 Views
Jul 07, 2020
12931 Views
Jul 07, 2020
8698 Views
Jul 04, 2020
19823 Views
Jul 04, 2020
9860 Views
Jul 04, 2020
6909 Views
Jul 04, 2020
3607 Views
Jul 03, 2020
10075 Views
Jul 01, 2020
8294 Views
Jul 01, 2020
5842 Views
Jun 30, 2020
13885 Views
Jun 30, 2020
10576 Views
Jun 23, 2020
7786 Views
Jun 17, 2020
8127 Views
Jun 16, 2020
6078 Views
Jun 12, 2020
9441 Views
Jun 09, 2020
7341 Views


























.png)
_(1).jpg)
_(2).jpg)
.jpg)

_(1).jpg)

.jpg)
.jpg)
.jpg)
.jpg)
.jpg)


.jpg)
_(1).jpg)
.jpg)
.jpg)


_(1).png)

.png)

1.png)





























4.png)









0 Replies to “What Is NumPy? NumPy With Python Introduction”
Leave a Reply
Your email address will not be published.