Part of the series

Several example codes

~2 min read • Updated Sep 17, 2025

Program Overview

This Python program performs the following operations using separate methods:
- read_array(): Reads integers from the user and stores them in a list
- reverse_array(arr): Reverses the list and returns the result
- print_array(arr): Prints the elements of the list
The program reads the array, reverses it, and displays the result.


Python Code:


def read_array():
    n = int(input("Enter the number of elements: "))
    arr = []
    for i in range(n):
        num = int(input(f"Enter element {i+1}: "))
        arr.append(num)
    return arr

def reverse_array(arr):
    return arr[::-1]

def print_array(arr):
    print("Array:")
    for item in arr:
        print(item, end=" ")
    print()

# Main program
original_array = read_array()
reversed_array = reverse_array(original_array)
print("Reversed array:")
print_array(reversed_array)

Sample Output:


Enter the number of elements: 5  
Enter element 1: 10  
Enter element 2: 20  
Enter element 3: 30  
Enter element 4: 40  
Enter element 5: 50  
Reversed array:  
50 40 30 20 10

Explanation:

Here’s how the program works:
- The read_array() function reads user input into a list
- The reverse_array() function uses slicing [::-1] to reverse the list
- The print_array() function prints the list elements with spacing


Written & researched by Dr. Shahin Siami