+ 3
Shocking in-place operators 🔥🔥🔥🔥😱😱👌
Recently I encountered this... (But can't tell why in-place operators work that way) Firstly without in-place operator >>>list=[] >>>list=list+"hello" >>>print(list) TypeError: can only concatenate list(not str) with list Now with in-place operator >>>list=[] >>>list+="hello" >>>print(list) ["h", "e", "l", "l", "o"] So list+="hello" is not the same as saying list=list+"hello"
2 Respuestas
+ 2
Adding a list with a string and then assigning it back to a list, is different from appending a string to a list.
While
x += 5
is commonly explained to be a shorthand of
x = x + 5
which is true, different languages provide different shorthands for different purposes. So happens that += is defined as such in Python.