Introduction to NumPy
Reviewed & published by Brayan K
NumPy is a Python library for fast numerical computing on arrays — it gives you a powerful n-dimensional array object and a huge collection of vectorized math functions that run in optimized C code.
Part of the free NumPy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this first lesson you'll learn what NumPy is, why it's so much faster than plain Python lists, and how to install and import it ready for the rest of the course.
What You'll Learn in This Lesson
- What NumPy is and where it's used
- Why NumPy arrays beat Python lists for speed
- How to install NumPy with pip
- The universal 'import numpy as np' convention
- Creating your very first array
- Doing vectorized math with no loops
1 What Is NumPy?
NumPy (short for Numerical Python) is the core library for numerical computing in Python. Its star feature is the ndarray — a grid of values, all of the same type, that can be one-dimensional (a list of numbers), two-dimensional (a table or matrix), or have many more dimensions.
Almost every data tool in Python — pandas, scikit-learn, TensorFlow, OpenCV, Matplotlib — is built on top of NumPy arrays. Learning NumPy unlocks the entire scientific Python ecosystem.
🧰 What NumPy gives you:
- A fast, compact n-dimensional array type
- Vectorized math that applies to every element at once
- Tools for linear algebra, random numbers, and statistics
- A bridge to C, C++, and Fortran for high performance
import numpy as np
# Create your first NumPy array from a Python list
arr = np.array([1, 2, 3, 4, 5])
print(arr) # [1 2 3 4 5]
print(type(arr)) # <class 'numpy.ndarray'>
# Vectorized math — every element is multiplied at once, no loop needed
print(arr * 2) # [ 2 4 6 8 10]
print(arr + 100) # [101 102 103 104 105]
# ✅ Expected output:
# [1 2 3 4 5]
# <class 'numpy.ndarray'>
# [ 2 4 6 8 10]
# [101 102 103 104 105]Expected output: the array [1 2 3 4 5], its type numpy.ndarray, then [ 2 4 6 8 10] and [101 102 103 104 105].
2 Why NumPy Beats Plain Python Lists
A Python list can hold any mix of types, which makes it flexible but slow. A NumPy array holds one type in contiguous memory, so operations run in compiled C — often 10–100× faster on large data.
import numpy as np
# The "Python list" way — needs a loop
prices = [10, 20, 30, 40]
with_tax_list = []
for p in prices:
with_tax_list.append(p * 1.2)
print(with_tax_list) # [12.0, 24.0, 36.0, 48.0]
# The NumPy way — one line, no loop
prices_arr = np.array([10, 20, 30, 40])
with_tax_arr = prices_arr * 1.2
print(with_tax_arr) # [12. 24. 36. 48.]
# ✅ Expected output:
# [12.0, 24.0, 36.0, 48.0]
# [12. 24. 36. 48.]Expected output: both print the same values, but the NumPy version is one line and far faster on big arrays: [12. 24. 36. 48.].
3 Installing and Importing NumPy
Install NumPy once from your terminal using pip:
pip install numpyThen import it at the top of every script. The whole community uses the alias np:
import numpy as np
print(np.__version__) # e.g. 1.26.4import numpy as np
# A 2D array (a matrix): a list of lists
grid = np.array([[1, 2, 3],
[4, 5, 6]])
print(grid)
# [[1 2 3]
# [4 5 6]]
print("Shape:", grid.shape) # Shape: (2, 3) -> 2 rows, 3 columns
print("Sum:", grid.sum()) # Sum: 21
# ✅ Expected output:
# [[1 2 3]
# [4 5 6]]
# Shape: (2, 3)
# Sum: 21Expected output: the 2×3 grid, Shape: (2, 3), and Sum: 21.
🎯 YOUR TURN: Fill in the Blanks
Replace each ___ so the program creates an array and triples every value.
import ___ as np # fill in the library name
data = np.___([5, 10, 15]) # fill in the function that builds an array from a list
tripled = data * ___ # fill in the number to triple each element
print(tripled) # Expected output: [15 30 45]Expected output: [15 30 45]. (Answers: numpy, array, 3.)
⚠️ Common Errors & Quick Reference
❌ ModuleNotFoundError: No module named 'numpy'
NumPy isn't installed in your environment.
✅ Fix: run pip install numpy (or pip3 install numpy).
❌ NameError: name 'np' is not defined
You forgot the import, or imported under a different name.
✅ Fix: add import numpy as np at the top of the file.
| Task | Code |
|---|---|
| Import NumPy | import numpy as np |
| Create an array | np.array([1, 2, 3]) |
| Check version | np.__version__ |
| Element-wise math | arr * 2 |
🏆 Mini Challenge: Temperature Converter
Convert a list of Celsius temperatures to Fahrenheit using a single vectorized expression.
import numpy as np
celsius = np.array([0, 10, 20, 30, 37])
# Formula: F = C * 9/5 + 32 — apply it to the whole array at once
fahrenheit = celsius * 9 / 5 + 32
print(fahrenheit) # [ 32. 50. 68. 86. 98.6]
# ✅ Expected output:
# [32. 50. 68. 86. 98.6]❓ Frequently Asked Questions
Lesson complete — you've met NumPy!
You now know what NumPy is, why its arrays are faster than lists, and how to install and import it. You even ran your first vectorized math without a single loop.
Practice quiz
What does NumPy stand for?
- Numerical Python
- Number Pyramid
- Numeric PyTorch
- New Python
Answer: Numerical Python. NumPy is short for Numerical Python, the core library for numerical computing.
What is the universal community alias for importing NumPy?
- nump
- np
- numpy
- npy
Answer: np. Everyone writes 'import numpy as np', so the np alias is expected everywhere.
What is NumPy's core array object called?
- list
- matrix
- ndarray
- dataframe
Answer: ndarray. The ndarray (n-dimensional array) is NumPy's star data structure.
Which command installs NumPy from the terminal?
- npm install numpy
- import numpy
- python numpy
- pip install numpy
Answer: pip install numpy. You install NumPy once with 'pip install numpy'.
Why is NumPy faster than plain Python lists for math?
- It uses more RAM
- It uses threads only
- It runs vectorized math in compiled C on contiguous memory
- It avoids importing
Answer: It runs vectorized math in compiled C on contiguous memory. Contiguous storage plus optimized C makes element-wise math 10-100x faster.
Given arr = np.array([1, 2, 3, 4, 5]), what does arr * 2 produce?
- [1 2 3 4 5 1 2 3 4 5]
- [2 4 6 8 10]
- 10
- an error
Answer: [2 4 6 8 10]. Vectorized multiplication doubles every element: [2 4 6 8 10].
What does grid.shape return for np.array([[1,2,3],[4,5,6]])?
- (2, 3)
- (3, 2)
- (6,)
- 6
Answer: (2, 3). Shape (2, 3) means 2 rows and 3 columns.
Applying one expression to every element with no loop is called what?
- Looping
- Recursion
- Vectorization
- Iteration
Answer: Vectorization. Vectorization applies an operation to the whole array at once at C speed.
Which error message means NumPy is not installed?
- SyntaxError
- ValueError
- TypeError
- ModuleNotFoundError: No module named 'numpy'
Answer: ModuleNotFoundError: No module named 'numpy'. A missing install raises ModuleNotFoundError; fix it with pip install numpy.
What does grid.sum() return for np.array([[1,2,3],[4,5,6]])?
- 6
- 15
- 21
- [6 15]
Answer: 21. Summing every element 1+2+3+4+5+6 gives 21.
Continue this course
- Next: The ndarray: NumPy's Core Object — Understand the n-dimensional array that powers all of NumPy
- Quick reference: NumPy cheat sheet › Creating Arrays
Frequently asked questions
What is NumPy used for?
NumPy is the foundation of numerical computing in Python. It provides fast, memory-efficient n-dimensional arrays and a huge library of vectorized math functions used in data science, machine learning, image processing, and scientific computing.
Is NumPy faster than plain Python lists?
Yes. NumPy stores data in contiguous memory and runs operations in optimized C code, so element-wise math on large arrays is often 10-100x faster than looping over a Python list.
How do I install and import NumPy?
Install it once with 'pip install numpy', then import it in every script with 'import numpy as np'. The 'np' alias is the universal community convention.
Do I need to know advanced math to use NumPy?
No. Basic Python and a little arithmetic are enough to start. You can learn the linear-algebra features later as you need them.