2

In the book Learning Python, there are two kinds of assignments:

list assignment:

[a,b] = [1,2]

tuple assignment:

a,b = [1,2]

I don't see any difference in result of this two kinds of assignments, is there any difference I don't know yet?

If there isn't, why are they called different names?

1
  • Not a dupe! Accepted answer in the suggested duplicate avoids answering this part of the (dup) question. (It simply shows that sometimes parens are required for the tuple assignment to work.) Commented Jan 5, 2018 at 23:55

1 Answer 1

1

They do the same thing. A tuple is a read-only version of a list. Usually you use parentheses (a, b) to create tuples versus square brackets [a, b] for lists, but the parentheses can sometimes be omitted. You could also write:

(a,b) = [1,2]

Or, perhaps most common:

a,b = 1,2
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you are right, I tried x = [a,b] = 1,2, x is (a,b) instead of [a,b].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.