Explanation:
Creating a list of pairs
pairs = [(1,2),(3,4),(5,6)]
pairs is a list containing three tuples.
Each tuple has two numbers: (x, y).
Initializing sum
s = 0
A variable s is created to store the running total.
Starts at 0.
Loop starts
for x, y in pairs:
The loop iterates over each tuple in pairs.
On each iteration:
x receives the first value.
y receives the second value.
Iterations:
(1, 2)
(3, 4)
(5, 6)
Modify x inside loop
x += y
Adds y to x.
This does not change the original tuple (tuples are immutable).
This only changes the local variable x.
Step-by-step:
x = 1 + 2 = 3
x = 3 + 4 = 7
x = 5 + 6 = 11
Add new x to sum
s += x
Adds the updated value of x into s.
Running total:
s = 0 + 3 = 3
s = 3 + 7 = 10
s = 10 + 11 = 21
Line 6 — Final output
print(s)
Displays the total sum.
Final Output:
21


0 Comments:
Post a Comment