NumPy: Create an array of all the even integers from 30 to 70
NumPy: Basic Exercise-15 with Solution
Write a NumPy program to create an array of all even integers from 30 to 70.
Sample Solution :
Python Code :
# Importing the NumPy library with an alias 'np'
import numpy as np
# Creating an array of even integers from 30 to 70 (inclusive) with a step size of 2 using np.arange()
array = np.arange(30, 71, 2)
# Printing a message indicating an array of all the even integers from 30 to 70
print("Array of all the even integers from 30 to 70")
# Printing the array of all the even integers from 30 to 70
print(array)
Sample Output:
Array of all the even integers from 30 to 70 [30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70]
Explanation:
In the above code -
np.arange(30,71,2): Here np.arange() function creates a NumPy array containing evenly spaced values within a specified interval. In this case, it generates an array starting at 30, ending before 71, with a step size of 2, which results in an array of even integers from 30 to 70.
Pictorial Presentation:
Python-Numpy Code Editor:
Previous: NumPy program to create an array of the integers from 30 to 70.
Next: NumPy program to create a 3x3 identity matrix.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics