21 AnswersNew Answer
The append method does not return a value at all. The  object is not assigned to a variable, so its modification is lost forever, because you have no chance to access the object.
List append does not return explicit value. Any function in python that does not return explicit value returns None implicitly, which is getting assigned to lst variable here which you are printing. You can try this, you will still get None lst =  lst = lst.append(5) print(lst) output = None
It's a convention. for every methods that mutate values, None should be returned.. just like, sort(), add(), append(), e.t.c
THEGreatGatsby method = function Everything in python is an object. This function returns 1. def func(): return 1 This function returns None def func(): print(1) All functions that doesn't have the return keyword would return None. I think append() is among
You can try this, it will work: lst =  #lst = lst.append(5) # <--- don't use this lst.append(5) print(lst) #output = [1,5]
Michael Which scenario is more correct in terms of what is happening. 1. The function returns an implicit None since there is no explicit return value or 2. The function does not return a value at all but since there is an assignment involved, the variable must be assigned something hence None is the best fit
~ swim ~ I quote the official Python documentation: "The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but None.” (https://docs.python.org/3/library/stdtypes.html) The Null object “is returned by functions that don’t explicitly return a value. It supports no special operations. There is exactly one null object, named None (a built-in name).” (https://docs.python.org/3/library/stdtypes.html#the-null-object)
Michael Thanks for the information. So it clearly returns implicit None. I asked since you said appends does not return any value at all so just had a bit of thought about what exactly is happening.
Yes the result would be same as none of those methods return explicit values
Michael "null" does not exists in python but people would say None == null In other programming language. If it returns nothing, it returns None. It can't return null Both link you shared above are explaining the same thing in a different way. Then note that the 'return None" can also be made deliberately a = lambda : None print(a) #None
Mirielle🐶 [Inactive] You are right, but I have never argued that there is a “null” in Python. Nevertheless there is a “Null object” in the meaning of a design pattern, which is an established term. It is also the terminology in the official Python documentation. The Null object in Python is implemented via the class NoneType which has None as its sole instance.
Vikas Jain Seriously ! The whole thread is about why the output is None. Then you are asking why the type of None is None? According to you what should be the type of None? Moreover you have already completed your python course !
This method changed the list immediately in place and it returns none, this works with sequences that are not constants. For example, you can take a str and a list, and append and replace methods, the append list method will change the existing list without creating a new one, and the str method will return a new str with the changes
but in our case we are appening value 5 ? so why not 5 for answe?
do you mean that if append with value would return value in case lst?
Yes cos it's a convention. append(), sort(), add() etc are functions that returns None.
Also try to avoid typing little L because you can easily confuse it with a big i. I <—> l (One can read your code as 1st or as the short form of List)
Oh am sorry. i raised my query in higher window.will surely delete it.pls ignore.by the way thanks for the reply.i got my answer in your reply.
so if we replace append with sort(),add() the result would be the same ? Is that correct?
what method is returning explicit value in python?