In Python, the array data structure is used to store the collection of data with the same data type. The list which is also referred to as a dynamic array is used to create the array in Python. The “Numpy” module also provides a function named “array()” which is used to create the array. To print the array the “print()” method and “for loop” method are used in Python.
In this post, we will explain the possible methods to print an array in Python:
Method 1: Using print() Method
The “print()” method is utilized to display the particular message on the screen. Using the “print()” method, we can print the array elements. Let’s understand it via the following examples:
Example 1: Printing Normal Array
The “print()” method is used in the below code to print the given arrays named “array_1d” and “array_2d”:
Code:
array_1d = [55, 45, 85, 95, 100]
print("1D Array: ", array_1d)
array_2d = [[45, 55, 25],[0, 10, 20, 30]]
print("2D-array: ", array_2d)
In the above code:
- The “1-D” array and “2-D” array are initialized in the program.
- The “print()” function takes the given arrays and displays them on the screen.
Output:
The array has been displayed on the screen.
Example 2: Printing a Numpy Array
The given below code is used to print the “Numpy” array:
Code:
import numpy
array_1d = numpy.array([55, 45, 85, 95, 100])
print("1D Array: ", array_1d)
array_2d = numpy.array([[45, 55, 25,67],[90, 10, 20, 30]])
print("2D-array: ", array_2d)
In the above code:
- The “numpy.array()” is used to create the “1-D” and “2-D” array.
- The “print()” function accepts the numpy array as an argument and displays it on the console screen.
Output:
The output verified that the Numpy array had been shown on the screen.
Method 2: Using for Loop
The traditional “for” loop can also be used to print an array in Python. The following examples demonstrate how to print an array using the “for” loop:
Example 1: Printing an Array
The given below code is used to print the array using for loop:
Code:
array_1d = [55, 45, 85, 95, 100]
print("1D Array: ")
for item in array_1d:
print(item, end = ' ')
array_2d = [[45, 55, 25,67],[90, 10, 20, 30]]
print("\n\n2D Array: :")
for item in array_2d:
for i in item:
print(i, end=" ")
print()
In the above code:
- A “1-D” and a “2-D” array is initialized in the program.
- The “for loop” iterates over each element of the “1-D” and “2-D” array and the print() function is used to print the array elements.
Output:
The input arrays have been printed on the screen.
Example 2: Printing a Numpy Array
The below code shows how to print the Numpy Array by using the traditional “for” loop:
Code:
import numpy
array_1d = numpy.array([55, 45, 85, 95, 100])
print("1D Array: ")
for item in array_1d:
print(item, end = ' ')
array_2d = numpy.array([[45, 55, 25,67],[90, 10, 20, 30],[9, 5, 7, 4]])
print("\n\n2D Array: :")
for item in array_2d:
for i in item:
print(i, end=" ")
print()
In the above code:
- The “Numpy” module provides a function named “np.array()” to create a “1-D” and “2-D” array.
- The “for” loop iterates over the initialized array and the “print()” function is used to print the elements of the array.
Output:
The “1-D” and “2-D” arrays have been printed on the screen.
Conclusion
To print the simple array or numpy array the “print()” method and traditional “for loop” is used in Python. The “numpy.array()” function creates the numpy array. The “print(array)” function is used to print the entire array on screen. While on the other hand the “for loop” iterates over each array item and prints the array elements one by one. This guide delivered a precise guide on how to print an array in Python using various examples.