NumPy Getting Started

๐Ÿš€ NumPy โ€” Getting Started

NumPy is a Python library used for working with arrays, numbers, and mathematical operations. Before using NumPy, 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

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)

Output:

[1 2 3 4 5]

๐Ÿ” 4. Checking NumPy Version

import numpy as np

print(np.__version__)


๐Ÿงฎ 5. NumPy Arrays vs Python Lists

Python list:

my_list = [1, 2, 3, 4, 5]

NumPy array:

my_array = np.array([1, 2, 3, 4, 5])

NumPy arrays are:

โœ” Faster
โœ” Require less memory
โœ” Allow vectorized arithmetic operations

Example:

print(my_array * 2)

Output:

[ 2 4 6 8 10]

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


๐Ÿ“š 6. Creating Different Types of Arrays

โžค Array of zeros

np.zeros(5)

Output:

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

โžค Array of ones

np.ones((2, 3))

Output:

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

โžค Generate sequence (like Python range())

np.arange(1, 10, 2)

Output:

[1 3 5 7 9]

โžค Generate evenly spaced values

np.linspace(0, 1, 5)

Output:

[0. 0.25 0.5 0.75 1. ]

๐Ÿ“ 7. Checking Array Attributes

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr.ndim) # No. of dimensions
print(arr.shape) # Rows & columns
print(arr.size) # Total elements


โœ” Summary

Task Example
Install NumPy pip install numpy
Import NumPy import numpy as np
Create array np.array([1,2,3])
Special arrays np.zeros(), np.ones(), np.arange()
Check version np.__version__

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *