Can anyone explain why??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone explain why???

Why [1, 2, 3] cannot be assigned to literal? Under which circumstances can do? How to revise the code pls? https://code.sololearn.com/c19sENSAyj9N/?ref=app

6th Sep 2022, 2:54 PM
Philmocoucou
Philmocoucou - avatar
3 Answers
+ 8
an assignment can be done to: > an existing python object > to a literal that is a valid identifier name. this is the case when objects are created and initialized with values the assignment target is always on the left side of the assignment operator in your case, [1, 2, 3] is an unvalid identifier name, see python docs.
6th Sep 2022, 3:27 PM
Lothar
Lothar - avatar
+ 5
Technically, a tuple is a single object. And you tried assigning a literal value which can't be done. What is your desired outcome?
6th Sep 2022, 3:15 PM
Slick
Slick - avatar
+ 4
# The value of different datatypes kan be repesenting through some kind of notation, like: 5 "hello world" (1, 3) {'a': 10, 'b': 20} # These values can be stored in variables, like: k = 5 s = "hello world" t = (1, 3) d = {'a': 10, 'b': 20} # So on the left side on the assignment operator = , you have the variables, and on the right side the symbols for the values of a specific datatype, like numbers, strings, tuples or dictionaries. # You can't save a variable in a litteral, that is in a value, as you tried to do. Always the opposite: # <LEFT: variables> = <RIGHT: litterals> # Some changes in your code: numbers = (1, 2, 3) a, b, c = numbers print(a) print(b) print(c) #my code bits numbers = (a, b, c) [n1, n2, n3] = numbers print(f"{n1 = }, {n2 = }, {n3 = }")
6th Sep 2022, 4:22 PM
Per Bratthammar
Per Bratthammar - avatar