Printing an array in Python is a common task that you might need to do when working with Python. An *array* is a collection of elements of the same data type which are stored together in contiguous memory locations. The best way to *print an array* in Python is by using a for
loop.
Steps to Print an Array in Python
1. First, create an array in Python. You can create an array in Python by using the array
module.
1 2 |
import array arr = array.array('i', [1, 2, 3, 4, 5]) |
2. Use a for
loop to iterate over the elements in the array and print them out.
1 2 |
for i in arr: print(i) |
3. Run the program and you will see the output.
1 2 3 4 5
Conclusion
Printing an array in Python is easy if you use a for
loop to iterate over the elements in the array. Hopefully, this tutorial has helped you learn how to print an array in Python.
1 2 3 4 5 |
import array arr = array.array('i', [1, 2, 3, 4, 5]) for i in arr: print(i) |