Home > Enterprise >  How is this happening? python
How is this happening? python

Time:06-21

my_list= [(0,1,2),(3,4,5),(6,7,8)]
for a,b,c in my_list:
    print(a)
    print(b)
    print(c)
my_list = a b c
print(my_list) 

I run this and it comes as 21 for last i would just like to know how that comes thanks again!

CodePudding user response:

In the last iteration a = 6, b = 7 and c = 8, hence it is giving you 21.

CodePudding user response:

What you are doing is iterating through your array, and unpacking the tuple into a,b,c.

The last tuple is (6,7,8) and when it maps to (a,b,c) it is unpacked to a=6, b=6, c=8.

What you call my_list will not be a list, but the sum of a,b and c.

  • Related