Python program that uses a for loop to print the elements of an array:

Created with Sketch.

Python program that uses a for loop to print the elements of an array:

def print_array(arr):
    for i in arr:
        print(i)

# Example usage
arr = [1, 2, 3, 4, 5]
print_array(arr)

The function print_array(arr) takes an array as an argument. It uses a for loop to iterate over the array, and for each element, it prints the element.

The example usage creates an array and calls the print_array() function to print the elements of the array.

You could also use join() method along with a loop to print the elements of an array

def print_array(arr):
    print(" ".join(map(str,arr)))

This method uses the join() method to join the elements of the array after converting them to strings using the map() function with str as the first argument, and arr as the second argument. Finally, it prints the joined elements.

Also, you could use print() function with the * operator to unpack the elements of the array and print them.

def print_array(arr):
    print(*arr)

This method uses the * operator to unpack the elements of the array and pass them as separate positional arguments to the print() function.

Leave a Reply

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