FAQ: Learn Python: Function Arguments - Default Return

The more I read here in discussion forum, the more I am getting confused.
Till now, my understanding was ,

  1. every python function should have 'return ’ statement otherwise it will print None.
  2. In other words, a function without an explicit ‘return’ statement/keyword returns ‘None’.

Here, in this below code:
List is sorted with the help of sort() and returned.

>>> k = [14, 631, 4, 51358, 50000000]
>>> k.sort()
>>> k
[4, 14, 631, 51358, 50000000]

In this below code:
After sorting the value is stored to a variable ‘l’ and printed.
It is not returned, so it is printing None.

>>> k = [14, 631, 4, 51358, 50000000]
>>> l = k.sort()
>>> l
>>> print(l)

Is my understanding right? Or am I missing something.
Looking forward to few inputs so that I cover in-depth meaning of Default return topic.

Many thanks in advance.

>>> a = 42
>>> a

Notice how None doesn’t even take a line?

Yes, Now I understood the true meaning of ‘None’

‘None’ == ‘No Value at all’.
‘None’ != zero / empty string/ False

None is a datatype of its own (NoneType) and only None can be None .


1 Like

Be sure to add the understanding that methods very often return, None. They have an execution context upon which to operate. In situ, as it were.

x.sort()     #  Nonassignable.

takes place on x.

sorted(x)    #  Assignable.

takes place on a copy of x.

1 Like

hey @mtf

“What’s in common with these two functions that return None ? They both have side-effects besides returning a value.”

can you explain this a little?
(in Default Return)

Any function that performs in place will have no return value expected save for the default return, None.

Please post a link to the lesson where you found that narrative.


1 Like

That lesson compares the print() function to the list.sort() method, which is also a function. Both of them return None every time. Their respective side effects are sending the argument to the standard output (console) and sorting the array in object context.

I am sorry I am having trouble with the last sentence

>>> print ("this expression")
this expresssion            #  side effect
>>> a = [6,2,8,3,1,9,4]
>>> a.sort()
>>> a
[ 1, 2, 3, 4, 6, 8, 9 ]     #  side effect