Python Program on NumPy Array Creation

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In this article, we will explore a Python program that demonstrates various methods of the array module. The array module provides an efficient way to store and manipulate arrays in Python. The program covers essential methods like pop(), tolist(), append(), index(), remove(), count(), reverse(), and insert(). Understanding these methods is crucial for effective array manipulation in Python.

Prerequisites

  • Basic Python Knowledge (Variables, Data Types, Syntax)
  • Familiarity with the NumPy Library

Topic Explanation

In this program, an array of integers is created using the array module as a foundation for demonstrating its diverse functionalities. The demonstration covers methods like pop(), which removes the array’s last element, and tolist(), converting the array into a list. It also includes append(), which adds new elements to the array, enhancing its utility.

The exploration extends to methods like index(), which identifies the position of an element, and remove(), which deletes an element’s first appearance in the array. For quantifying the frequency of elements, the count() method is employed. Additionally, the program showcases reverse(), for inverting the array’s order, and insert(), allowing for the placement of elements at specific locations, thus broadening the array’s adaptability.

Code:

# Importing the NumPy library with an alias 'np' for ease of use
import numpy as np

# Constructing an integer array 'myar' with a customized set of values
myar = np.array([5, 15, 25, 35, 10, 20, 30, 40], int)

#Outputting the mean value of all elements in the array
print("Average:", myar.mean())

# Generating an array 'myar' with 4 values linearly spaced between 1 and 4
myar = np.linspace(1, 4, 4)

# Creating a sequence 'myar' with values from 5 to 15 in ascending order
myar = np.arange(5, 16)

# Outputting the created ascending sequence
print("Ascending sequence:", myar)

# Forming an array 'myar' of 5 elements, all set to 2 and of float type
myar = np.full(5, 2.0)

# Displaying the array with all elements set to 2
print("Array of twos:", myar)

# Constructing an array 'myar' of 5 elements, all set to 3 and of float type
myar = np.full(5, 3.0)

# Showing the array with all elements set to 3
print("Array of threes:", myar)

Output:

Average: 22.5
Ascending sequence: [ 5 6 7 8 9 10 11 12 13 14 15]
Array of twos: [2. 2. 2. 2. 2.]
Array of threes: [3. 3. 3. 3. 3.]

Code Explanation:

  • Imports numpy library and aliases it as np
  • Creates an integer numpy array ‘myar’ with elements: 10, 3, 40, 50, 20, 10, 6, 7 using np.array()
  • Prints the sum of all elements in myar
  • Creates a float numpy array ‘myar’ with 5 evenly spaced elements from 0 to 10 using np.linspace()
  • Creates a numpy array ‘myar’ with values from 10 to 0 in steps of -1 (descending) using np.arange()
  • Prints myar
  • Creates a float numpy array ‘myar’ with 10 zeroes using np.zeros()
  • Prints myar
  • Creates a float numpy array ‘myar’ with 10 ones using np.ones()
  • Prints myar

Summary

In summary, this Python program offers a hands-on introduction to creating arrays using the versatile NumPy library, a fundamental tool in Python’s data science ecosystem. By demonstrating various NumPy array creation functions and showcasing their practical applications, this program equips readers with essential skills for manipulating data efficiently.

Proficiency in NumPy is highly valuable across diverse domains, such as scientific computing, data analysis, and machine learning, where it forms the foundation for advanced data manipulation and mathematical operations, empowering individuals to tackle complex real-world challenges effectively.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *