-2

I am fairly new to python and I just learned about tuple unpacking, so I was playing around with this concept and got a weird result.

nList = [(1,2),4,5,6]
for a in nList:
    print(a)
    print(b)

I was expecting my program to crash since b is not defined as a placeholder and even if it was only the first element of my list is a tuple, but instead, I got the following result:

(1, 2)
2
4
2
5
2
6
2
Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33

1 Answers1

0

You have not initialized the variable b in your code, because of which it is using some garbage value of b from memory (which is 2). To avoid such scenarios you must initialize your variables with some value (or 0) in start of your code before using them.

5400824
  • 71
  • 3
  • 10