Line-by-line Explanation:
-
total = 0
→ This initializes a variable total with the value 0. -
for i in range(1, 5):
→ This loop runs with i taking values 1, 2, 3, 4.
(Remember: range(start, stop) includes start but excludes stop.) -
total += i
→ In each iteration, it adds the current i to total.
Here's how it goes:-
First iteration (i = 1): total = 0 + 1 = 1
-
Second iteration (i = 2): total = 1 + 2 = 3
-
Third iteration (i = 3): total = 3 + 3 = 6
-
Fourth iteration (i = 4): total = 6 + 4 = 10
-
-
print(total)
→ After the loop, it prints the final value of total, which is **10**.
✅ Output:


0 Comments:
Post a Comment