Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How To Import Numpy As Np
Next article icon

How To Import Numpy As Np

Last Updated : 23 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore how to import NumPy as 'np'. To utilize NumPy functionalities, it is essential to import it with the alias 'np', and this can be achieved by following the steps outlined below.

What is Numpy?

NumPy stands for Numerical Python supports large arrays and matrices and can write advanced arithmetic operations that operate on arrays. NumPy's powerful implementation in C and Fortran makes NumPy the library of choice for computing in Python. It is a Python library that helps us work with the arrays. It also has functions for working in the domain of linear algebra, Fourier transform, and matrices.

Import Numpy as Np

Below, we will explain step-by-step how to import NumPy as np in Python.

Step 1: Create a Virtual Environment

First, create the virtual environment using the below commands

python -m venv env 
.\env\Scripts\activate.ps1

Step 2: Install NumPy Library

Before using NumPy, it is necessary to install the NumPy library by executing the following command in the terminal:

pip install numpy
Screenshot-2024-01-17-125546
Installing numpy

Step 3 : Import NumPy as Np

Once NumPy is installed, you can import it into your Python script or interactive environment. The standard convention is to use the alias "np" for NumPy. This not only makes your code more concise but also follows a widely adopted practice in the Python community.

import numpy as np

Step 4: Check 'np' is Imported using Code

Example 1: NumPy and Array Creation

In this example, below code imports the NumPy library with the alias "np" and demonstrates creating a NumPy array and calculating its mean using the "np" shorthand for concise code.

Python3
import numpy as np  # Now you can use np to reference  # NumPy functions and objects arr = np.array([1, 2, 3, 4, 5]) print("Array:", arr)  # Example of using a NumPy  #function with the np alias mean_value = np.mean(arr) print("Mean:", mean_value) 

Output :

Array: [1 2 3 4 5]
Mean: 3.0

Example 2: NumPy for 1D and 2D Array Creation

In this example, the np.array() function is used to create NumPy arrays. The first example creates a 1-dimensional array, while the second example creates a 2-dimensional array (matrix).

Python3
import numpy as np  # Creating a 1-dimensional array arr_1d = np.array([1, 2, 3]) print("1D Array:\n", arr_1d)  # Creating a 2-dimensional array (matrix) arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) print("\n2D Array:") print(arr_2d) 

Output :

1D Array:
[1 2 3]
2D Array:
[[1 2 3]
[4 5 6]]

Conclusion

In conclusion, utilizing the import numpy as np statement simplifies the integration of the NumPy library into Python scripts or interactive environments. The adoption of the widely accepted alias "np" enhances code readability and conciseness. This convention streamlines the referencing of NumPy functions and objects, providing a cleaner and more efficient coding experience.

Related Article

  • NumPy Tutorial
  • Introduction to NumPy

Next Article
How To Import Numpy As Np

A

adarshmaster
Improve
Article Tags :
  • Python
  • Numpy
Practice Tags :
  • python

Similar Reads

    How To Save Multiple Numpy Arrays
    NumPy is a powerful Python framework for numerical computing that supports massive, multi-dimensional arrays and matrices and offers a number of mathematical functions for modifying the arrays. It is an essential store for Python activities involving scientific computing, data analysis, and machine
    3 min read
    How to Install Numpy on Linux?
    Python NumPy is a general-purpose array processing package that provides tools for handling n-dimensional arrays. It provides various computing tools such as comprehensive mathematical functions, linear algebra routines. NumPy provides both the flexibility of Python and the speed of well-optimized c
    2 min read
    How to Fix: No module named NumPy
    In this article, we will discuss how to fix the No module named numpy using Python. Numpy is a module used for array processing. The error "No module named numpy " will occur when there is no NumPy library in your environment i.e. the NumPy module is either not installed or some part of the installa
    2 min read
    Import Text Files Into Numpy Arrays - Python
    We have to import data from text files into Numpy arrays in Python. By using the numpy.loadtxt() and numpy.genfromtxt() functions, we can efficiently read data from text files and store it as arrays for further processing.numpy.loadtxt( ) - Used to load text file datanumpy.genfromtxt( ) - Used to lo
    3 min read
    How to Fix "Can't Import Numpy in VSCode" in Python?
    NumPy is a general-purpose array-processing Python library that provides handy methods and functions for working with n-dimensional arrays. NumPy is short for “Numerical Python” and offers various computing tools such as comprehensive mathematical functions and linear algebra routines. VSCode (Visua
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences