-3

My friend gave my this code in Python:

a ,= 1------1,
print(a)

The output is 2. But why? Can somebody explain how it works?

dan
  • 1
  • 1

1 Answers1

2

a, = 1------1, is the same as: a = 1------1

1-(-(-(-(-(-1)))))
 |+| |+| |+|

=> 1 + 1 = 1

You can think of this comma like this:

a = 1 # a is 1
a, = [1] # a is 1
a = [1] # a is [1]
Daniel
  • 1,426
  • 1
  • 11
  • 24