How to use np.arange?

0134

The np.arange function in NumPy is used to create an array of evenly spaced values within a specified range. Here's how to use it:

Syntax

numpy.arange([start, ]stop[, step])

Parameters

  • start (optional): The starting value of the sequence (default is 0).
  • stop: The end value of the sequence (exclusive).
  • step (optional): The spacing between values (default is 1).

Examples

  1. Basic Usage

    import numpy as np
    x = np.arange(10)
    # Output: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  2. Specifying Start and Stop

    y = np.arange(5, 15)
    # Output: array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
  3. Specifying Step

    z = np.arange(0, 10, 2)
    # Output: array([0, 2, 4, 6, 8])

Use Cases

  • Creating sequences of numbers for loops or data generation.
  • Useful in plotting and simulations.

Feel free to experiment with different parameters to see how np.arange behaves! If you want to learn more, consider checking out relevant labs on LabEx.

0 Comments

no data
Be the first to share your comment!