w3resource

NumPy: Create a 1-D array going from 0 to 50

NumPy: Array Object Exercise-44 with Solution

Write a NumPy program to create a 1-D array going from 0 to 50 and an array from 10 to 50.

Pictorial Presentation:

Python NumPy: Create a 1-D array going from 0 to 50

Sample Solution:-

Python Code:

import numpy as np
x = np.arange(50)
print("Array from 0 to 50:")
print(x)
x = np.arange(10, 50)
print("Array from 10 to 50:")
print(x)

Sample Output:

Array from 0 to 50:                                                    
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 2
3 24                                                                   
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 4
8 49]                                                                  
Array from 10 to 50:                                                   
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3
3 34                                                                   
 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to create a 2-D array whose diagonal equals [4, 5, 6, 8] and 0's elsewhere.
Next: Write a NumPy program to Create a 1-D array of 30 evenly spaced elements between 2.5. and 6.5, inclusive.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]