I have two sets of code for taking the list A=[1,2,3] and changing A into [1,2,3,4].
Here is the first code, followed by what it prints.
A=[1,2,3]
A.append(4)
print(type(A))
print(A)
<class 'list'>
[1, 2, 3, 4]
This first code works.
Here is the second code, followed by what it prints.
A=[1,2,3]
A=A.append(4)
print(type(A))
print(A)
<class 'NoneType'>
None
This second code returns None.
- What is the explanation for this and why the second code is "wrong"?
- It seems that x=f(x,y) is fine if f is a function but x=x.method(y) is completely different.
For example, suppose I want to turn 'no no no' into 'yeah yeah yeah':
text='no no no'
text=text.replace('no','yeah')
text
'yeah yeah yeah'
text='no no no'
text.replace('no','yeah')
text
'no no no'
So now the situation is reversed. So you just have to learn which one is correct for each method?