reverse array python
Reversing an Array in Python
To reverse an array in Python, you can use slicing. Here's an example:
array = [1, 2, 3, 4, 5]
reversed_array = array[::-1]
print(reversed_array)
Explanation of each step:
- Create an array:
array = [1, 2, 3, 4, 5]
. - Use slicing to reverse the array:
reversed_array = array[::-1]
. The[::-1]
notation means to start from the end of the array and move backwards with a step size of -1, effectively reversing the order of the elements. - Print the reversed array:
print(reversed_array)
.
The output will be [5, 4, 3, 2, 1]
.
That's it! This is a simple and concise way to reverse an array in Python using slicing.