Code Explanation:
Line 1: Create a List
a = [1, 2, 3]
Explanation
a is a variable.
[1, 2, 3] is a list containing three elements.
The list is stored in memory.
Index Positions
Index Value
0 1
1 2
2 3
Negative Indexes
Negative Index Value
-1 3
-2 2
-3 1
Line 2: Access the Element
print(a[-0])
Step 1: Evaluate -0
Explanation
In Python, -0 is the same as 0.
Example:
print(-0)
Output
0
Step 2: Replace -0 with 0
a[-0]
becomes
a[0]
Step 3: Access the First Element
Python accesses index 0.
a = [1, 2, 3]
↑
Index 0
Value at index 0 is:
1
Step 4: Print the Value
print(1)
Output
1
Final Output
1

0 Comments:
Post a Comment