Printing an array in python – In Python, How do you Print an Array?

Python Array:

Printing an array in python: An array in Python is a linear data structure that stores similar data items in contiguous memory locations.

When compared to a List which is a dynamic Array, Python Arrays keeps elements of the same datatype. A Python List, on the other hand, can store entries of several/different data types.

An array can be either 1-Dimensional or Multi-Dimensional

There are various ways to implement an array in Python. They are:

  • Using for loop and range() Function
  • Using Numpy Module
  • Using list

Printing an array involves displaying each of its elements correctly. We can accomplish this by employing the below methods:

  • By explicitly passing the complete array to the print() method,
  • 2. by using for loop and formatting

An array can also be multidimensional. Two-dimensional arrays are, as we all know, the most basic type of multi-dimensional array. As a result, in this course, we will look at both 1D and 2D arrays.

Printing an Array

1)Using Lists

a)Printing an Array with print() Function:

Print an array in python: To print the values from an array(list), we can directly pass the name of the array(list) to Python’s print() function.

However, in this example, the array is written as a list, with brackets and values separated by commas.

Example

Approach:

  • Give the one-dimensional array(as a list) as static input and store it in a variable.
  • Give the Two-dimensional array as static input and store it in another variable.
  • Print the given one-dimensional Array using the print() method.
  • Print the given Two-dimensional Array using the print() method.
  • The Exit of the Program.

Below is the implementation:

# Give the one-dimensional array(as list) as static input and store it in a variable.
gvn_1D_arry = [13, 45, 7, 2, 5]
# Give the Two-dimensional array as static input and store it in another variable.
gvn_2D_arry = [[7, 8], [7, 10]]
# Print the given one-dimensional Array using the print() method
print("The given one-dimensional Array = ", gvn_1D_arry)
# Print the given Two-dimensional Array using the print() method
print("The given Two-dimensional Array is:\n", gvn_2D_arry)

Output:

The given one-dimensional Array =  [13, 45, 7, 2, 5]
The given Two-dimensional Array is:
 [[7, 8], [7, 10]]

Here the arrays are passed in the form of lists.

b)Printing an Array with For Loop:

Print an array python: In Python, we can also print an array by traversing all of its elements with for loops.

Example

# Give the one dimensional array(as list) as static input and store it in a variable.
gvn_1D_arry = [13, 45, 7, 2, 5]
# Give the Two dimensional array as static input and store it in another variable.
gvn_2D_arry = [[7, 8], [7, 10]]
# Print the given one-dimensional Array
print("The given one-dimensional Array = ")
# Loop in the given one-dimensional Array using the for loop
for itr in gvn_1D_arry:
    # Inside the for loop print the value of iterator separated by spaces
    print(itr, end=' ')
print()

# Print the given Two-dimensional Array
print("The given Two-dimensional Array is:")
# Loop in the given Two-dimensional Array using the two nested for loops
for m in gvn_2D_arry:
    for n in m:
        # Print the iterator value of the inner fop loop separated by spaces.
        print(n, end=" ")
    print()

Output:

The given one-dimensional Array = 
13 45 7 2 5 
The given Two-dimensional Array is:
7 8 
7 10

2)Using Numpy Array

a)Printing an Array with print() Function:

How to print an array in python: arrays can also be implemented in Python using the NumPy module. The module includes a pre-defined array class that can store values of the same datatypes

These NumPy arrays can be multidimensional as well. Here we see how we can print both 1D and 2D NumPy arrays in Python.

Example

# Import numpy module as np using the import Keyword
import numpy as np 
# Give the one-dimensional array as static input using the array() function 
# and store it in a variable.
gvn_1D_arry = np.array([13, 45, 7, 2, 5])
# Give the multi-dimensional(3D) array as static input using the array() function
#  and store it in another variable.
gvn_3D_arry = np.array([[7, 8], [9, 10],[11, 12]])
# Print the given one-dimensional Array using the print() function
print("The given one-dimensional Array = \n",gvn_1D_arry)
# Print the given Three-dimensional Array using the print() function
print("The given Three-dimensional Array =\n",gvn_3D_arry)

Output:

The given one-dimensional Array = 
[13 45 7 2 5] 
The given Three-dimensional Array = 
[[ 7 8] 
 [ 9 10] 
 [11 12]]

Note: It’s worth noting that the arrays are still printed in the manner of NumPy arrays with brackets.

b)Printing an Array with For Loop:

Python print an array: Again, we can use for loop structures to traverse NumPy arrays in Python. As a result, we may access and print each element in the array. This is yet another method for printing an array in Python.

Example

# Import numpy module as np using the import Keyword
import numpy as np 
# Give the one-dimensional array as static input using the array() function 
# and store it in a variable.
gvn_1D_arry = np.array([13, 45, 7, 2, 5])
# Give the multi-dimensional(2D) array as static input using the array() function
#  and store it in another variable.
gvn_2D_arry = np.array([[7, 8], [9, 10]])

# Print the given one-dimensional Array using the print() function
print("The given one-dimensional Array = ")
# Loop in the given one-dimensional Array using the for loop
for itr in gvn_1D_arry:
    # Inside the for loop print the value of iterator separated by spaces
    print(itr, end=' ')
print()
# Print the given Two-dimensional Array using the print() function
print("The given Two-dimensional Array =")
# Loop in the given Two-dimensional Array using the two nested for loops
for m in gvn_2D_arry:
    for n in m:
        # Print the iterator value of the inner fop loop separated by spaces.
        print(n, end=" ")
    print()


Output:

The given one-dimensional Array = 
13 45 7 2 5 
The given Two-dimensional Array = 
7 8 
9 10