Skip to content
    geeksforgeeks
    • Interview Prep
      • DSA
      • Interview Corner
      • Aptitude & Reasoning
      • Practice Coding Problems
      • All Courses
    • Tutorials
      • Python
      • Java
      • ML & Data Science
      • Programming Languages
      • Web Development
      • CS Subjects
      • DevOps
      • Software and Tools
      • School Learning
    • Tracks
      • Languages
        • Python
        • C
        • C++
        • Java
        • Advanced Java
        • SQL
        • JavaScript
        • C#
      • Interview Preparation
        • GfG 160
        • GfG 360
        • System Design
        • Core Subjects
        • Interview Questions
        • Interview Puzzles
        • Aptitude and Reasoning
        • Product Management
        • Computer Organisation and Architecture
      • Data Science
        • Python
        • Data Analytics
        • Complete Data Science
        • Gen AI
        • Agentic AI
      • Dev Skills
        • Full-Stack Web Dev
        • DevOps
        • Software Testing
        • CyberSecurity
        • NextJS
        • Git
      • Tools
        • Computer Fundamentals
        • AI Tools
        • MS Excel & Google Sheets
        • MS Word & Google Docs
      • Maths
        • Maths For Computer Science
        • Engineering Mathematics
        • School Maths
    • DSA
    • Practice Problems
    • C
    • C++
    • Java
    • Python
    • JavaScript
    • Data Science
    • Machine Learning
    • Courses
    • Linux
    • DevOps
    • SQL
    • Web Development
    • System Design
    • Aptitude
    Open In App

    Different Ways to Create Numpy Arrays in Python

    Last Updated : 23 Jul, 2025
    Comments
    Improve
    Suggest changes
    3 Likes
    Like
    Report

    Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods.

    Ways to Create Numpy Arrays

    Below are some of the ways by which we can create NumPy Arrays in Python:

    Create Numpy Arrays Using Lists or Tuples

    The simplest way to create a NumPy array is by passing a Python list or tuple to the numpy.array() function. This method creates a one-dimensional array.

    Python3
    import numpy as np
    
    my_list = [1, 2, 3, 4, 5]
    numpy_array = np.array(my_list)
    print("Simple NumPy Array:",numpy_array)
    

    Output
    [1 2 3 4 5]
    

    Initialize a Python NumPy Array Using Special Functions

    NumPy provides several built-in functions to generate arrays with specific properties.

    • np.zeros(): Creates an array filled with zeros.
    • np.ones(): Creates an array filled with ones.
    • np.full(): Creates an array filled with a specified value.
    • np.arange(): Creates an array with values that are evenly spaced within a given range.
    • np.linspace(): Creates an array with values that are evenly spaced over a specified interval.
    Python3
    import numpy as np
    
    zeros_array = np.zeros((2, 3))
    ones_array = np.ones((3, 3))
    constant_array = np.full((2, 2), 7)
    range_array = np.arange(0, 10, 2)  # start, stop, step
    linspace_array = np.linspace(0, 1, 5)  # start, stop, num
    
    print("Zero Array:","\n",zeros_array)
    print("Ones Array:","\n",ones_array)
    print("Constant Array:","\n",constant_array)
    print("Range Array:","\n",range_array)
    print("Linspace Array:","\n",linspace_array)
    

    Output
    Zero Array 
     [[0. 0. 0.]
     [0. 0. 0.]]
    Zero Array 
     [[1. 1. 1.]
     [1. 1. 1.]
     [1. 1. 1.]]
    Constant Array 
     [[7 7]
     [7 7]]
    Range Array 
     [0 2 4 6 8]
    Linspace Array 
     [0.   0.25 0.5  0.75 1.  ]
    

    Create Python Numpy Arrays Using Random Number Generation

    NumPy provides functions to create arrays filled with random numbers.

    • np.random.rand(): Creates an array of specified shape and fills it with random values sampled from a uniform distribution over [0, 1).
    • np.random.randn(): Creates an array of specified shape and fills it with random values sampled from a standard normal distribution.
    • np.random.randint(): Creates an array of specified shape and fills it with random integers within a given range.
    Python3
    import numpy as np
    
    random_array = np.random.rand(2, 3)
    normal_array = np.random.randn(2, 2)
    randint_array = np.random.randint(1, 10, size=(2, 3))  
    
    print(random_array)
    print(normal_array)
    print(randint_array)
    

    Output
    [[0.87948864 0.55022063 0.29237533]
     [0.99475413 0.76666244 0.55240304]]
    [[ 1.77971899  0.67837749]
     [ 0.33101208 -1.04029635]]
    [[6 6 3]
     [8 5 8]]
    

    Create Python Numpy Arrays Using Matrix Creation Routines

    NumPy provides functions to create specific types of matrices.

    • np.eye(): Creates an identity matrix of specified size.
    • np.diag(): Constructs a diagonal array.
    • np.zeros_like(): Creates an array of zeros with the same shape and type as a given array.
    • np.ones_like(): Creates an array of ones with the same shape and type as a given array.
    Python3
    import numpy as np
    
    identity_matrix = np.eye(3)
    diagonal_array = np.diag([1, 2, 3])
    zeros_like_array = np.zeros_like(diagonal_array)
    ones_like_array = np.ones_like(diagonal_array)
    
    print(identity_matrix)
    print(diagonal_array)
    print(zeros_like_array)
    print(ones_like_array)
    

    Output
    [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]
    [[1 0 0]
     [0 2 0]
     [0 0 3]]
    [[0 0 0]
     [0 0 0]
     [0 0 0]]
    [[1 1 1]
     [1 1 1]
     [1 1 1]]
    
    Create Quiz

    R

    rahulsanketpal0431
    Improve

    R

    rahulsanketpal0431
    Improve
    Article Tags :
    • Numpy
    • Python-numpy

    Explore

      Introduction

      NumPy Introduction

      5 min read

      Python NumPy

      6 min read

      NumPy Array in Python

      2 min read

      Basics of NumPy Arrays

      4 min read

      Numpy - ndarray

      3 min read

      Data type Object (dtype) in NumPy Python

      3 min read

      Creating NumPy Array

      Numpy - Array Creation

      5 min read

      numpy.arange() in Python

      2 min read

      numpy.zeros() in Python

      2 min read

      NumPy - Create array filled with all ones

      2 min read

      NumPy - linspace() Function

      2 min read

      numpy.eye() in Python

      2 min read

      Creating a one-dimensional NumPy array

      2 min read

      How to create an empty and a full NumPy array

      2 min read

      Create a Numpy array filled with all zeros - Python

      2 min read

      How to generate 2-D Gaussian array using NumPy?

      2 min read

      How to create a vector in Python using NumPy

      4 min read

      Python - Numpy fromrecords() method

      2 min read

      NumPy Array Manipulation

      NumPy Copy and View of Array

      4 min read

      How to Copy NumPy array into another array?

      2 min read

      Appending values at the end of an NumPy array

      4 min read

      How to swap columns of a given NumPy array?

      4 min read

      Insert a new axis within a NumPy array

      4 min read

      numpy.hstack() in Python

      2 min read

      numpy.vstack() in python

      2 min read

      Joining NumPy Array

      3 min read

      Combining a One and a Two-Dimensional NumPy Array

      3 min read

      Numpy np.ma.concatenate() method-Python

      2 min read

      Numpy dstack() method-Python

      2 min read

      Splitting Arrays in NumPy

      6 min read

      How to compare two NumPy arrays?

      2 min read

      Find the union of two NumPy arrays

      2 min read

      Find unique rows in a NumPy array

      3 min read

      Numpy np.unique() method-Python

      2 min read

      numpy.trim_zeros() in Python

      2 min read
    top_of_element && top_of_screen < bottom_of_element) || (bottom_of_screen > articleRecommendedTop && top_of_screen < articleRecommendedBottom) || (top_of_screen > articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
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
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Campus Training Program
  • Explore
  • POTD
  • Job-A-Thon
  • Blogs
  • Nation Skill Up
  • Tutorials
  • Programming Languages
  • DSA
  • Web Technology
  • AI, ML & Data Science
  • DevOps
  • CS Core Subjects
  • Interview Preparation
  • Software and Tools
  • Courses
  • ML and Data Science
  • DSA and Placements
  • Web Development
  • Programming Languages
  • DevOps & Cloud
  • GATE
  • Trending Technologies
  • Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
  • Preparation Corner
  • Interview Corner
  • Aptitude
  • Puzzles
  • GfG 160
  • System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
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.
See More

What kind of Experience do you want to share?

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