Explanation:
1. List Initialization
lst = [1, 2, 3, 4]
A list named lst is created.
It contains 4 elements: 1, 2, 3, 4.
๐น 2. For Loop Setup
for i in range(len(lst)):
len(lst) gives the length of the list → 4.
range(4) generates numbers: 0, 1, 2, 3.
The loop runs 4 times with i as the index:
First iteration → i = 0
Second → i = 1
Third → i = 2
Fourth → i = 3
๐น 3. Updating List Elements
lst[i] += i
This means:
๐ lst[i] = lst[i] + i
Let’s see each iteration:
Iteration i Original lst[i] Calculation New Value
1 0 1 1 + 0 1
2 1 2 2 + 1 3
3 2 3 3 + 2 5
4 3 4 4 + 3 7
๐น 4. Final List After Loop
After all updates:
lst = [1, 3, 5, 7]
๐น 5. Printing the Result
print(lst)
Outputs:
[1, 3, 5, 7]
✅ Final Output
[1, 3, 5, 7]

0 Comments:
Post a Comment