numpy complete tutorial
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
NumPy Complete Tutorial (Beginner → Advanced)
This notebook provides a structured and correct tutorial covering NumPy from basics to advanced usage.
Sections include:
- Installation & Setup
- NumPy Arrays vs Python Lists
- Array Creation
- Array Attributes
- Indexing & Slicing
- Boolean Indexing
- Fancy Indexing
- Broadcasting
- Vectorization
- Aggregations
- Linear Algebra
- Random Numbers
- Statistics
- Reshaping
- Stacking & Splitting
- File IO
- Structured Arrays
- Performance Optimization
- Memory Layout
- Practical Examples
import numpy as np
print("NumPy version:", np.__version__)
NumPy Arrays vs Python Lists
Explanation and example.
import numpy as np
python_list=[1,2,3,4]
numpy_array=np.array([1,2,3,4])
print("Python list:",python_list)
print("NumPy array:",numpy_array)
print("List * 2:",python_list*2)
print("Array * 2:",numpy_array*2)
Exercise
Modify the example and experiment with larger arrays.
Array Creation
Explanation and example.
import numpy as np
a=np.array([1,2,3])
b=np.zeros((3,3))
c=np.ones((2,4))
d=np.arange(0,10,2)
e=np.linspace(0,1,5)
print(a)
print(b)
print(c)
print(d)
print(e)
Exercise
Modify the example and experiment with larger arrays.
Array Attributes
Explanation and example.
import numpy as np
x=np.random.rand(3,4)
print("Shape:",x.shape)
print("Dimensions:",x.ndim)
print("Size:",x.size)
print("Datatype:",x.dtype)
Exercise
Modify the example and experiment with larger arrays.
Indexing and Slicing
Explanation and example.
import numpy as np
x=np.arange(10)
print(x[0])
print(x[-1])
print(x[2:7])
print(x[::2])
Exercise
Modify the example and experiment with larger arrays.
Boolean Indexing
Explanation and example.
import numpy as np
x=np.arange(10)
mask=x>5
print(mask)
print(x[mask])
Exercise
Modify the example and experiment with larger arrays.
Fancy Indexing
Explanation and example.
import numpy as np
x=np.arange(10)
indices=[1,3,5,7]
print(x[indices])
Exercise
Modify the example and experiment with larger arrays.
Reshaping Arrays
Explanation and example.
import numpy as np
x=np.arange(12)
reshaped=x.reshape(3,4)
print(reshaped)
Exercise
Modify the example and experiment with larger arrays.
Broadcasting
Explanation and example.
import numpy as np
a=np.array([1,2,3])
b=np.ones((3,3))
print(a)
print(b)
result=a+b
print(result)
[1 2 3]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[2. 3. 4.]
[2. 3. 4.]
[2. 3. 4.]]
Exercise
Modify the example and experiment with larger arrays.
Vectorized Operations
Explanation and example.
import numpy as np
x=np.arange(5)
print(np.sin(x))
print(np.exp(x))
print(np.sqrt(x))
[ 0. 0.84147098 0.90929743 0.14112001 -0.7568025 ]
[ 1. 2.71828183 7.3890561 20.08553692 54.59815003]
[0. 1. 1.41421356 1.73205081 2. ]
Exercise
Modify the example and experiment with larger arrays.
Aggregations
Explanation and example.
import numpy as np
x=np.random.rand(4,4)
print("Sum:",np.sum(x))
print("Mean:",np.mean(x))
print("Min:",np.min(x))
print("Max:",np.max(x))
Exercise
Modify the example and experiment with larger arrays.
Linear Algebra
Explanation and example.
import numpy as np
A=np.array([[1,2],[3,4]])
B=np.array([[5,6],[7,8]])
print("Matrix multiplication")
print(A@B)
print("Determinant")
print(np.linalg.det(A))
print("Inverse")
print(np.linalg.inv(A))
Exercise
Modify the example and experiment with larger arrays.
Random Numbers
Explanation and example.
import numpy as np
print(np.random.rand(5))
print(np.random.randn(5))
print(np.random.randint(0,10,(3,3)))
Exercise
Modify the example and experiment with larger arrays.
Statistics
Explanation and example.
import numpy as np
x=np.random.rand(100)
print("Mean:",np.mean(x))
print("Median:",np.median(x))
print("Std:",np.std(x))
print("Variance:",np.var(x))
Exercise
Modify the example and experiment with larger arrays.
Stacking Arrays
Explanation and example.
import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
print(np.vstack([a,b]))
print(np.hstack([a,b]))
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
Exercise
Modify the example and experiment with larger arrays.
Splitting Arrays
Explanation and example.
import numpy as np
x=np.arange(10)
print(np.split(x,2))
[array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]
Exercise
Modify the example and experiment with larger arrays.
Saving and Loading Files
Explanation and example.
import numpy as np
x=np.arange(10)
np.save("array.npy",x)
loaded=np.load("array.npy")
print(loaded)
Exercise
Modify the example and experiment with larger arrays.
Structured Arrays
Explanation and example.
import numpy as np
data=np.array([
(1,"Alice",25),
(2,"Bob",30)
],dtype=[("id","i4"),("name","U10"),("age","i4")])
print(data["name"])
['Alice' 'Bob']
Exercise
Modify the example and experiment with larger arrays.
Performance: Vectorization vs Loops
Explanation and example.
import numpy as np
import time
x=np.arange(1000000)
start=time.time()
y=[i*i for i in x]
print("Python loop:",time.time()-start)
start=time.time()
y=x*x
print("NumPy vectorized:",time.time()-start)
Exercise
Modify the example and experiment with larger arrays.
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →