Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

NumPy Environment and IDE Setup

Learn how to configure your Python environment, choose an appropriate code editor, select the correct interpreter, and run NumPy programs successfully.

1. Learning Objectives

By the end of this lesson, you will be able to:

  • Explain what a Python environment is.
  • Understand the purpose of an integrated development environment (IDE).
  • Choose a suitable editor for writing NumPy programs.
  • Create and activate a virtual environment.
  • Select the correct Python interpreter in an IDE.
  • Run and debug a basic NumPy program.
  • Troubleshoot common environment configuration problems.

2. What Is a Python Environment?

A Python environment is the setup used to run Python programs. It includes a Python interpreter and may include installed packages, configuration settings, and project-specific dependencies.

Different projects may require different package versions. A separate environment helps you manage these requirements without unnecessarily changing packages used by other projects.

Example: Two Different Projects

Suppose you are working on two projects. One project uses NumPy for numerical calculations, while another depends on a different combination of Python packages.

Creating separate virtual environments lets each project maintain its own installed dependencies.

Python Interpreter Project A Virtual Environment A NumPy and project packages Project B Virtual Environment B Independent dependencies Separate environments help isolate project dependencies.
Figure 1: Separate virtual environments help keep project dependencies independent.
Tip: A virtual environment is especially useful when you work on multiple Python projects or need reproducible package configurations.

3. What Is an IDE?

An Integrated Development Environment, or IDE, is an application that provides tools for writing, running, testing, and debugging programs. Many IDEs also provide code completion, project navigation, terminal access, and interpreter selection.

Tool Typical Use Beginner Consideration
Visual Studio Code Editing Python files with extensions and integrated terminal support. Requires the appropriate Python extension and interpreter selection.
PyCharm Python-focused development, debugging, and project management. Provides dedicated Python development features.
Jupyter Notebook Interactive code cells, experimentation, and data analysis. Useful for testing calculations step by step.
IDLE Basic Python editing and interactive execution. Simple option for learning fundamental Python concepts.
Important: Installing an IDE does not always mean NumPy is installed in the Python environment selected by that IDE. You must check the interpreter and its packages.

4. Create a Project Folder

Keeping your source files organized makes a Python project easier to understand and maintain. Create a dedicated folder for your NumPy practice work.

Example Project Structure

NumPyPractice/
|
|-- .venv/
|
|-- main.py
|
|-- arrays_practice.py
|
|-- notes.txt
|
|-- requirements.txt

In this example:

  • .venv/ contains the virtual environment.
  • main.py contains the main practice program.
  • arrays_practice.py contains additional exercises.
  • notes.txt stores learning notes.
  • requirements.txt records project package requirements.
Best practice: Do not manually edit files inside the virtual environment folder. Use Python and pip commands to manage that environment.

5. Create a Virtual Environment

Python's built-in venv module can create a virtual environment. Run the following commands from your project folder.

Step 1: Create the Environment

python -m venv .venv

On systems where Python is accessed using python3, use:

python3 -m venv .venv

Step 2: Activate on Windows

For Command Prompt:

.venv\Scripts\activate.bat

For PowerShell:

.\.venv\Scripts\Activate.ps1

Step 3: Activate on macOS or Linux

source .venv/bin/activate

Step 4: Install NumPy

python -m pip install numpy
PowerShell note: If activation is blocked by your system's execution policy, you can use the environment's Python executable directly or follow your organization's approved PowerShell policy. Do not weaken security settings without understanding the implications.

6. Select the Correct Python Interpreter

An IDE must use the Python interpreter associated with the environment where NumPy is installed. Selecting a different interpreter can cause an import error even when NumPy is installed elsewhere on the computer.

General Steps in an IDE

  1. Open your NumPy project folder.
  2. Locate the Python interpreter or environment settings.
  3. Select the interpreter inside your project's .venv folder.
  4. Open the integrated terminal.
  5. Confirm the terminal is using the intended environment.
  6. Run a test program that imports NumPy.

The exact menu names vary by editor and version, so use the interpreter or environment selection feature provided by your chosen IDE.

Check the Active Interpreter

import sys

print(sys.executable)

The output shows the executable path of the Python interpreter running the program. Check that it points to the environment you intended to use.

7. Create and Run Your First IDE Program

Create a Python file named main.py in your project folder. Add the following code.

import sys
import numpy as np

print("Python executable:", sys.executable)
print("NumPy version:", np.__version__)

values = np.array([10, 20, 30, 40, 50])

print("Values:", values)
print("Total:", np.sum(values))
print("Average:", np.mean(values))

Example output:

Python executable: [path to your Python interpreter]
NumPy version: [installed NumPy version]
Values: [10 20 30 40 50]
Total: 150
Average: 30.0

The first two output values depend on your computer. The numerical results should match the supplied array.

Tip: Run the file using your IDE's run command or its integrated terminal. If you use the terminal, ensure that the correct environment is active.

8. Debugging and Troubleshooting

Problem Likely Cause Recommended Check
ModuleNotFoundError: No module named 'numpy' NumPy is not installed in the active environment. Run python -m pip show numpy using the selected interpreter.
Terminal and IDE behave differently They may use different Python interpreters. Print sys.executable in both contexts.
Activation command fails Incorrect shell, path, or activation command. Check the operating system and shell instructions.
Unexpected package version Another environment is active or the package was upgraded. Check np.__version__ and the package location.
Program runs from the wrong folder The working directory is different from the project folder. Check the terminal's current directory and IDE run configuration.

Useful Diagnostic Commands

python -c "import sys; print(sys.executable)"

python -m pip show numpy

python -c "import numpy as np; print(np.__version__)"

python -c "import numpy as np; print(np.__file__)"

Run these commands with the Python interpreter you want to troubleshoot. This makes it easier to identify the active interpreter and the NumPy installation it can access.

9. Save Project Dependencies

Recording package requirements helps you recreate a project environment later. If NumPy is installed in your active environment, you can create a requirements file.

python -m pip freeze

To save the installed package list to a file, run:

python -m pip freeze > requirements.txt

To install packages recorded in that file in another appropriately configured environment, run:

python -m pip install -r requirements.txt
Important: The freeze command may record every package installed in the active environment. For a small teaching project, review the file before sharing it and ensure that its package requirements are appropriate.

10. Classroom Practical Activity

Activity: Prepare a NumPy Development Environment

  1. Create a folder named NumPyPractice.
  2. Open that folder in your chosen code editor.
  3. Create a virtual environment named .venv.
  4. Activate the environment using the correct command for your shell.
  5. Install NumPy using pip.
  6. Select the environment's interpreter in your IDE.
  7. Create a file named main.py.
  8. Run the program below and verify the numerical output.

Practice Program

import sys
import numpy as np

sales = np.array([1200, 1500, 1800, 2000])

print("Python:", sys.executable)
print("NumPy:", np.__version__)
print("Sales:", sales)
print("Total sales:", np.sum(sales))
print("Average sales:", np.mean(sales))

Expected numerical output:

Sales: [1200 1500 1800 2000]
Total sales: 6500
Average sales: 1625.0

Practice Questions

  1. What is a Python environment?
  2. Why are virtual environments useful?
  3. What is the role of an IDE?
  4. Why should an IDE use the correct Python interpreter?
  5. Which command creates a virtual environment named .venv?
  6. How can you check the active Python executable?
  7. What is the purpose of a requirements.txt file?

Summary

In this lesson, you learned how to organize a Python project, create and activate a virtual environment, install NumPy, and select the correct interpreter in your IDE.

You also learned how to run a NumPy program, inspect the active Python executable, troubleshoot import errors, and record package requirements for future use.