I have writtern the below codes
a = [1,2,3]
b = a.append(4)
Why there is nothing inside b
?
Because append
doesn't return the modified list - it simply adds the new element, modifying the list in-place and returning None
. To get the effect you want, do this instead:
b = a + [4]
Notice that the above creates a new list, whereas using append
modifies the same list.
I am not 100% sure what it is you want the value of b to be. But here is what it is when you run the code.
The value of b is None, because it is the return value of the method (.append()) Every method will return a value. In this case it is None, because a.append(4) is appending to the array a. Returning a value means to SIMPLY return a value. This doesn't require a value.
append returns None, That's why there is nothing in b. Your list a will be modified and added 4.
If you realy want to add 4 in b then you can try a + [4]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.