w3resource

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.

This NumPy program creates an array containing all even integers from 30 to 70. Utilizing NumPy's array creation and slicing functions, it efficiently generates the array with the specified sequence of even numbers. This program demonstrates how to filter and construct arrays with specific criteria in a concise and effective way.

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) 

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.

Visual Presentation:

NumPy: Create an array of all the even integers from 30 to 70.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/basic/numpy-basic-exercise-15.php