Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
numpy.zeros() in Python
Next article icon

numpy.arange() function creates an array of evenly spaced values within a given interval. It is similar to Python's built-in range() function but returns a NumPy array instead of a list.

Let's understand with a simple example:

Python
importnumpyasnp#create an arrayarr=np.arange(5,10)print(arr)

Output
[5 6 7 8 9]

Syntax of numpy.arange():

numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)

Parameters of numpy():

  • start (optional):The starting value of the sequence. Default is 0.
  • stop (required):The endpoint of the sequence, exclusive.
  • step (optional): The spacing between consecutive values. Default is 1.
  • dtype (optional):The desired data type of the output array.

Return Type:

  • Array of evenly spaced values.

Specify Start and Stop

Generate a sequence of integers starting from 5 to 14.

Python
importnumpyasnp# Creating a sequence of numbers from 0 to 9sequence=np.arange(10)print("Basic Sequence:",sequence)

Output
Basic Sequence: [0 1 2 3 4 5 6 7 8 9]

Floating-Point Step Size

Generate a sequence of floating-point numbers.

Python
importnumpyasnp# Creating a sequence of floating-point numbers from 0 to 1# with a step size of 0.2 using np.arange()sequence=np.arange(0,1,0.2)print("Floating-Point Sequence:",sequence)

Output
Floating-Point Sequence: [0.  0.2 0.4 0.6 0.8]

Combining with Conditional Filtering

Generate a sequence and filter specific values.

Python
importnumpyasnp# Creating a sequence of numbers from 0 to 20sequence=np.arange(0,20,3)# Filtering the sequence to include only values greater than 10filtered=sequence[sequence>10]print("Filtered Sequence:",filtered)

Output
Filtered Sequence: [12 15 18]

M

Mohit Gupta
Improve

Similar Reads

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 ourCookie 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

[8]ページ先頭

©2009-2025 Movatter.jp