Installing and Importing NumPy

Introduction

Before using NumPy in Python, we need to install it and import it into our program. In this chapter, we will learn how to Installing and Importing NumPy using different methods and how to start working with it.

Installing NumPy

NumPy can be installed using Python’s package manager, pip.

Open your Command Prompt or terminal and run this command:

pip install numpy

This command downloads and installs NumPy in your Python environment.

Installing NumPy Using Jupyter Notebook

If you are working in Jupyter Notebook, run this command in a notebook cell:

%pip install numpy

The %pip command installs NumPy in the Python environment used by the notebook.

How to Check NumPy Installation?

After installation, you can check whether NumPy is installed by running:

pip show numpy

This command displays information about the installed NumPy package, including its version.

Importing NumPy in Python

After installing NumPy, we need to import it before using its functions.

import numpy

Now, you can access NumPy functions using the numpy name.

For example:

import numpy

numbers = numpy.array([10, 20, 30, 40])

print(numbers)

Output:

[10 20 30 40]

Importing NumPy Using an Alias

Python allows us to give a shorter name to an imported library. NumPy is commonly imported using the alias np.

import numpy as np

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

print(numbers)

Output:

[10 20 30 40]

Here, np is a short name for NumPy. This is the most commonly used import method.

Checking the NumPy Version

You can check the installed NumPy version using the __version__ attribute.

import numpy as np

print(np.__version__)

Example Output:

2.3.0

Note: The displayed version may be different depending on your installed NumPy version.

Student Task

Try these tasks on your computer:

  1. Install NumPy using pip.
  2. Import NumPy using import numpy.
  3. Import NumPy using the alias np.
  4. Create an array containing five numbers.
  5. Display your installed NumPy version.

Final Student Challenge: Install and Import NumPy

Write a Python program that:

  1. Imports NumPy using the alias np.
  2. Creates an array containing 5, 10, 15, 20, 25.
  3. Prints the array.
  4. Displays the NumPy version.

Expected Output:

[ 5 10 15 20 25]
2.3.0

The version shown here is an example. Your installed version may be different.

Key Takeaways

  • NumPy can be installed using pip install numpy.
  • Jupyter Notebook supports %pip install numpy.
  • The import statement makes NumPy available in a Python program.
  • np is the commonly used alias for NumPy.
  • np.__version__ displays the installed version.

Frequently Asked Questions (FAQs)

1. How do I install NumPy in Python?

Run pip install numpy in your Command Prompt or terminal.

2. How do I import NumPy in Python?

Use import numpy to import the library.

3. Why do we use import numpy as np?

It gives NumPy a shorter name, np, making code easier to write.

4. Is NumPy already installed in Python?

No, NumPy may need to be installed separately, depending on your Python environment.

5. How can I check the NumPy version?

Use print(np.__version__) after importing NumPy as np.

6. Can I install NumPy in Jupyter Notebook?

Yes, run %pip install numpy in a notebook cell.

7. What happens if NumPy is not installed?

Python displays a ModuleNotFoundError when you try to import NumPy.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top