Array Reversal - Python Programming Exercise

This Python program prompts the user to input 5 numbers and stores them in an array (or list in Python). After all the numbers are entered, the program displays them in reverse order. The program uses a simple loop to collect the numbers and stores them in a list, which is a flexible data structure for handling multiple values. Once the numbers are stored, the program employs Python's list slicing technique to reverse the order of the numbers and print them back to the user. This exercise demonstrates how to handle user input, store data in an array, and manipulate that data by reversing the order of the elements in a list. The ability to reverse a list using slicing is a key feature of Python, making tasks like this both efficient and easy to implement. By prompting the user for input, storing it dynamically, and then using simple array operations, this program highlights fundamental programming concepts like arrays, user input, and looping, all essential for mastering Python programming.

 Category

Arrays, Lists, and Strings

 Exercise

Array Reversal

 Objective

Develop a Python program to prompt the user for 5 numbers, store them in an array, and display them in reverse order.

 Example Python Exercise

 Copy Python Code
# Initialize an empty list to store the numbers
numbers = []

# Prompt the user for 5 numbers and store them in the list
for i in range(5):
    number = float(input(f"Enter number {i+1}: "))
    numbers.append(number)

# Display the numbers in reverse order
print("Numbers in reverse order:")
for num in reversed(numbers):
    print(num)

 Output

Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Enter number 5: 50
Numbers in reverse order:
50.0
40.0
30.0
20.0
10.0

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Array Lookup

    his Python program asks the user to input a specific number of floating-point numbers and stores them in a list. The program then repeatedly prompts the user to enter...

  •  Even Number Array

    This Python program prompts the user to enter 10 integer numbers and displays only the even numbers from the list. It utilizes a for loop to iterate through th...

  •  Positive and Negative Number Array

    This Python program prompts the user to enter 10 real numbers and calculates two averages: one for the positive numbers and another for the negative numbers...

  •  Numerous Numbers and Their Sum

    This Python program prompts the user to enter several numbers, calculating their sum as they go. The program continues to prompt the user for numbers until they enter...

  •  Matrix Array

    This Python programming exercise is ideal for practicing the handling of multidimensional arrays. In this case, the goal is to prompt the user for the marks of 20 students, ...

  •  Advanced Statistics

    This Python statistical program is a fantastic exercise for improving data handling and user interaction in programming. The program allows users to add new data...