Step-by-step explanation
1️⃣ Create the list
lst = [1, 2, 3]The list has 3 elements.
2️⃣ Loop using index
for i in range(len(lst)):len(lst) → 3
range(3) → 0, 1, 2
-
So i will be 0, then 1, then 2
3️⃣ Multiply value by its index
lst[i] = lst[i] * i| Index (i) | Value (lst[i]) | Calculation | New value |
|---|---|---|---|
| 0 | 1 | 1 × 0 | 0 |
| 1 | 2 | 2 × 1 | 2 |
| 2 | 3 | 3 × 2 | 6 |
After the loop, the list becomes:
[0, 2, 6]4️⃣ Print the result
print(lst)✅ Output:
[0, 2, 6]

0 Comments:
Post a Comment