Numphy and Array
Numphy and Array
Introduction to NumPy
• NumPy stands for Numerical Python, is an open-source Python
library that provides support for large, multi-dimensional arrays and
matrices
• It also has a collection of high-level mathematical functions to
operate on arrays. It was created by Travis Oliphant in 2005.
• NumPy is a general-purpose array-processing package.
• It provides a high-performance multidimensional array object and
tools for working with these arrays.
• It is the fundamental package for scientific computing with Python.
Features of NumPy
• NumPy has various features which make them popular over lists.
• Some of these important features include:
A powerful N-dimensional array object
Sophisticated functions
Tools for integrating C/C++ and Fortran code
Useful linear algebra, Fourier transform, and random number
capabilities
NumPy in Python can also be used as an efficient multi-dimensional
container
Why use NumPy?
Python List NumPy
Excellent, general-purpose containers, To improve speed, reduce memory
can have heterogeneous elements consumption, and offer a high-level
syntax for performing a variety of
common processing tasks
Quite fast when used to perform For large quantities of “homogeneous”
individual operations on a handful of (same-type) data to be processed on
elements the CPU
Python List
• A list is a sequence of values called items or elements.
• The elements can be of any type.
• Creating a list using list():
1) Create an empty list L1 = list()
2) Create a list with any 3 nos. L2 = list([10,20,30])
3) Create a list with 3 string elements L3 = list([“RED”,”Green”, “Blue”])
• Creating a list without list():
1) L1 = [10,20,30]
2) L2 = [“India”, ”USA”, “Italy”]
• A list with heterogeneous elements:
Accessing the Elements of a list
• Syntax:
• Name_of_variable_of_a_List[index]
• Eg.
• L1 = list([10,20,30,40,50])
• L1 The entire list will be displayed
• L1[0] The first element 10 will be displayed
• L1[3] The 4th element 40 will be displayed
• L1[-1] The last element 50 will be displayed
Array
• An array is a data structure capable to hold similar type of elements.
• It can be one dimensional or multi-dimensional (2, 3, …dimension)
• One Dimensional
• Two Dimensional
Contd.
• In NumPy, this idea of multi dimensional is generalized to an
arbitrary number of dimensions
• So the fundamental array class is called ndarray: it represents an “N-
dimensional array”
• Most NumPy arrays have the following restrictions:
All elements of the array must be of the same type of data.
Once created, the total size of the array can’t change.
The shape must be “rectangular”, not “jagged”; e.g., each row of a
two-dimensional array must have the same number of columns.
An Example
# import module
import numpy as np
#creating a array
arr = np.array([3,4,5,5])
print("Array :",arr)
Output:
Array: [3 4 5 5]