NumPy Getting Started

NumPy Tutorial

NumPy Getting Started

NumPy is a Python library used for working with arrays, numbers, and mathematical operations. Before NumPy Getting Started, you must install and import it.


 1. Installing NumPy

Using pip:

pip install numpy

Using conda:

conda install numpy

If NumPy is already installed, Python will recognize it during import.


 2. Importing NumPy

The standard way to import NumPy is:

import numpy as np

Using np is a shortcut so you don’t type numpy every time.


 3. Creating Your First NumPy Array


 

Output:

[1 2 3 4 5]

 4. Checking NumPy Version


 


 5. NumPy Arrays vs Python Lists

Python list:


NumPy array:


NumPy arrays are:

  • Faster
  •  Require less memory
  • Allow vectorized arithmetic operations

Example:


Output:

[ 2 4 6 8 10]

(You cannot do this easily with normal Python lists.)


 6. Creating Different Types of Arrays

 Array of zeros


Output:

[0. 0. 0. 0. 0.]

 Array of ones


Output:

[[1. 1. 1.]
[1. 1. 1.]]

Generate sequence (like Python range())


Output:

[1 3 5 7 9]

Generate evenly spaced values


Output:

[0. 0.25 0.5 0.75 1. ]

7. Checking Array Attributes


 


Summary

TaskExample
Install NumPypip install numpy
Import NumPyimport numpy as np
Create arraynp.array([1,2,3])
Special arraysnp.zeros(), np.ones(), np.arange()
Check versionnp.__version__

You may also like...