How do you display an array in python?
In this tutorial, we are going to learn how to print an array in Python. So before we get right into the topic, let us know a bit about Arrays in Python. Arrays are a collection of data elements of the same type under the same name. In Python, we can implement arrays using lists or the NumPy module. The NumPy module provides us with arrays of type Further, an array can be multi-dimensional. As we know, the simplest form of
multi-dimensional arrays is two-dimensional arrays. Hence, in this tutorial, we are going to be considering 1D as well as 2D Arrays. Now, let us look at some of the ways to print both 1D as well as 2D arrays in Python. Note: these arrays are going to be implemented using lists. We can directly pass the name of the array(list) containing the values to be printed to the But in this case, the array is printed in the form of a list i.e. with brackets and values separated by commas. Output: The Array is: [2, 4, 5, 7, 9] The 2D-Array is: [[1, 2], [3, 4]] Here, Using for loops in PythonWe can also print an array in Python by traversing through all the respective elements using Let us see how. arr = [2,4,5,7,9] arr_2d = [[1,2],[3,4]] #printing the array print("The Array is : ") for i in arr: print(i, end = ' ') #printing the 2D-Array print("\nThe 2D-Array is:") for i in arr_2d: for j in i: print(j, end=" ") print() Output: The Array is : 2 4 5 7 9 The 2D-Array is: 1 2 3 4 In the code above we traverse through the elements of a 1D as well as a 2D Array using for loops and print the corresponding elements in our desired form. Ways to print NumPy Array in PythonAs mentioned earlier, we can also implement arrays in Python using the NumPy module. The module comes with a pre-defined array class that can hold values of same type. These NumPy arrays can also be multi-dimensional. So, let us see how can we print both 1D as well as 2D NumPy arrays in Python. Using print() methodSimilar to the case of arrays implemented using lists, we can directly pass NumPy array name to the import numpy as np arr_2d = np.array([[21,43],[22,55],[53,86]]) arr = np.array([1,2,3,4]) print("Numpy array is: ", arr) #printing the 1d numpy array print("Numpy 2D-array is: ", arr_2d) #printing the 2d numpy array Output: Numpy array is: [1 2 3 4] Numpy 2D-array is: [[21 43] [22 55] [53 86]] Here, Using for loopsAgain, we can also traverse through NumPy arrays in Python using loop structures. Doing so we can access each element of the array and print the same. This is another way to print an array in Python. Look at the example below carefully. import numpy as np arr = np.array([11,22,33,44]) arr_2d = np.array([[90,20],[76,45],[44,87],[73,81]]) #printing the numpy array print("The Numpy Array is : ") for i in arr: print(i, end = ' ') #printing the numpy 2D-Array print("\nThe Numpy 2D-Array is:") for i in arr_2d: for j in i: print(j, end=" ") print() Output: The Numpy Array is : 11 22 33 44 The Numpy 2D-Array is: 90 20 76 45 44 87 73 81 Here also we print the NumPy array elements in our desired way(without brackets) by accessing the elements of the 1D and 2D array individually. ConclusionSo in this tutorial, we learned how to print an array in Python. I hope you now have a clear understanding of the topic. For any further question related to the topic, feel free to use the comments. References
How do you display an entire array in Python?We set the print options for the array to be maximum with the np. set_printoptions(threshold = sys. maxsize) function. We then printed the full array with the simple print() function in Python.
How do you display an array?public class Array { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element: array) { System.out.println(element); } } }. import java.util.Arrays; public class Array { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; System.out.println(Arrays.toString(array)); } }. How do you print an array in Python?STEP 1: Declare and initialize an array. STEP 2: Loop through the array by incrementing the value of i. STEP 3: Finally, print out each element of the array.
How do you access an array in Python?Overview. An array in Python is used to store multiple values or items or elements of the same type in a single variable. We can access elements of an array using the index operator [] . All you need do in order to access a particular element is to call the array you created.
|