[Tutor] Assignment of an object to a target list

Alan Gauld alan.gauld at yahoo.co.uk
Wed Aug 12 09:08:45 EDT 2020


On 12/08/2020 10:38, Manprit Singh wrote:

> Just need to know that the following examples are correct  usage or not.

The easiest way is to try them in the interpreter - its why its there.

>>> a,  b,  c =  ["A",  "B",  "C"]
>>> a,b,c
('A', 'B', 'C')
>>> a,  b,  c = ("A",  "B",  "C")
>>> a,b,c
('A', 'B', 'C')
>>> a,  b,  c = {"A",  "B",  "C"}
>>> a,b,c
('A', 'C', 'B')        <------ Note ORDER
>>> a, b, c = range(3)
>>> a,b,c
(0, 1, 2)
>>> a, b, c = {"A":1, "B":2, "C":3}
>>> a,b,c
('A', 'B', 'C')
>>> a, b, c = "ABC"
>>> a,b,c
('A', 'B', 'C')
>>>

The only oddity is the set because sets are not ordered.
So you may not get the order that you expect.
Also true of dicts in older versions of Python (<3.6?)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list