1️⃣ List Creation
x = [1,2,3]
This creates a list named x containing three elements:
[1, 2, 3]
2️⃣ List Slicing
x[::-1]
This uses Python slicing syntax:
list[start : stop : step]
Here:
start = default (beginning)
stop = default (end)
step = -1
A step of -1 means move backward through the list.
So Python reads the list from end to start.
3️⃣ Reversing the List
Original list:
[1, 2, 3]
Reading backward:
[3, 2, 1]
4️⃣ Print Statement
print(x[::-1])
This prints the reversed list.
✅ Final Output
[3, 2, 1]
🔥 Important Point
[::-1] is a Python shortcut to reverse a list, string, or tuple.
Example:
text = "python"
print(text[::-1])
Output:


0 Comments:
Post a Comment