Yandex

Course IndexCourse Index0

    ProgramGuru

    Creating NumPy Arrays
    Examples


    If you've ever worked with Python lists and felt they weren’t fast or flexible enough, NumPy arrays are your new best friend. In this tutorial, you'll learn how to create different types of NumPy arrays—from basic 1D arrays to more complex structured ones. Let’s build this up slowly and clearly.

    1. Importing NumPy

    Before diving in, make sure NumPy is installed and imported.

    import numpy as np

    Note: We use np as a convention so we don't have to type numpy every time.

    2. Creating Arrays from Python Lists

    This is the most basic and intuitive way to create a NumPy array.

    arr = np.array([1, 2, 3, 4])

    You can verify the creation by printing its type:

    print(type(arr))  # <class 'numpy.ndarray'>

    3. Multi-Dimensional Arrays

    Nested lists help create 2D and 3D arrays.

    matrix = np.array([[1, 2], [3, 4]])

    Check: Make sure all inner lists (rows) have the same number of elements, or you'll get unexpected results.

    4. Using np.arange()

    Think of this as the NumPy version of Python’s built-in range().

    arr = np.arange(0, 10, 2)  # [0, 2, 4, 6, 8]

    Parameters: start, stop (exclusive), step

    5. Using np.linspace()

    Use this when you need evenly spaced numbers across an interval.

    arr = np.linspace(0, 1, 5)  # [0. , 0.25, 0.5 , 0.75, 1. ]

    Tip: Great for graphs or simulations requiring fixed step precision.

    6. Creating Arrays Filled with Zeros or Ones

    zeros = np.zeros((2, 3))   # 2x3 matrix of 0s
    ones = np.ones((3, 2))     # 3x2 matrix of 1s

    Shape must be passed as a tuple. Be cautious of forgetting the parentheses.

    7. Creating an Array with a Constant Value

    filled = np.full((2, 2), 7)  # Every element is 7

    8. Identity Matrix with np.eye()

    Useful in linear algebra.

    identity = np.eye(3)

    This creates a 3x3 identity matrix (diagonal = 1, else 0).

    9. Creating Empty Arrays (Be Cautious)

    empty_arr = np.empty((2, 2))

    Warning: The contents are uninitialized garbage values. Only use this when you're going to overwrite values immediately.

    Verification Tips

    • Use print(arr.shape) to check dimensions.
    • Use arr.dtype to see the data type.
    • Use arr.ndim to check array depth (1D, 2D, etc.).

    Common Pitfalls to Avoid

    • Forgetting to import NumPy
    • Mixing lists with different lengths (for multidimensional arrays)
    • Passing integers instead of tuples for shape parameters (e.g., np.zeros(2,3) is invalid)

    Summary

    Creating arrays is the very foundation of working with NumPy. Whether you're initializing data, simulating values, or preparing matrices for computation—there's a creation method tailored for it.

    As you continue through the course, you’ll see how these arrays are used for data analysis, simulations, and machine learning pipelines.

    Quick Recap

    • np.array() — creates numpy array from list
    • np.arange() — creates evenly spaced values
    • np.linspace() — creates evenly spaced over an interval
    • np.zeros(), np.ones(), np.full() — pre-filled arrays
    • np.eye() — creates identity matrix
    • np.empty() — creates uninitialized array


    Welcome to ProgramGuru

    Sign up to start your journey with us

    Support ProgramGuru.org

    You can support this website with a contribution of your choice.

    When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

    PayPal

    UPI

    PhonePe QR

    MALLIKARJUNA M