1

how does this script works and why the variable b get 50 as its value and not 1

a = 1
b = 50
b, b = a, b
print(b)

actual result: 50

1
  • 1
    Because the right-hand side is fully evaluated before the assignment. You can think of it as creating a tuple (a, b) == (1, 50) first Commented Apr 9, 2019 at 20:45

1 Answer 1

2

b, b = a, b is actually a tuple assignment, and it works from left to right.

b, b = a, b evaluates to (b, b) = (1, 50) which in turn is executed as

b = 1
b = 50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.