[Tutor] Assignment of an object to a target list
Manprit Singh
manpritsinghece at gmail.com
Wed Aug 12 05:38:43 EDT 2020
Dear all,
I have an iterable of three elements . i want to assign first value of the
iterable to name a, second value to name b and third value to name c .
Just need to know that the following examples are correct usage or not.
a, b, c = ["A", "B", "C"]
# List is an iterable, will assign "A" to name a , "B" to name b, "C" to
name c.
a, b, c = ("A", "B", "C")
# Tuple is also iterable , will assign
"A" to name a , "B" to name b, "C" to name c.
a, b, c = {"A", "B", "C"}
# Set as iterable, will assign "A" to name a , "B" to name b, "C" to
name c.
a, b, c = range(3)
# range as an iterable, will assign 0 to name a , 1 to name b, 2 to
name c.
a, b, c = {"A":1, "B":2, "C":3} #
Dict as iterable, will assign "A" to name a , "B" to name b, "C" to name c.
a, b, c = (i for i in range(1, 7) if (i/2).is_integer()) #
Generator expression(that returns an iterator) , will assign 2 to name a ,
4 to name b, 6 to name c.
a, b, c =
"ABC" #
Strings are iterables, will assign "A" to name a , "B" to name b, "C" to
name c.
Regards
Manprit Singh
More information about the Tutor
mailing list