FAQ: Learn Python: Function Arguments - Default Return

This community-built FAQ covers the “Default Return” exercise from the lesson “Learn Python: Function Arguments”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Default Return

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: This is my code

Hey guys!
This might be a question related to another topic, but it came to me at this exercise.
What does that dot actually do, it changes everything in their return:

list_sort_return = sorted(sort_this_list)


list_sort_return = sort_this_list.sort()

I understand what each code line does, and been using it for a little time already, but tell me please what does that dot do?

why isn’t it, for example:

list_sort_return = sort_this_list.sorted()


list_sort_return = sort(sort_this_list)

Thx, in advance :slight_smile:

This might be better in the general Get-help Python category but I don’t know that it’s possible to move it.

If you’ve not yet done the method on classes in Python then this description might not be too clear. You can read up about OOP and Python classes in particular or you can hold out until you reach that lesson. I wouldn’t stress about it too much until you’ve covered those topics.

The dot indicates an attribute of the object. In this case the object is a list and lists have a method called sort that adjust the of the list in place according to the key argument you provide it.

Sorted is a funciton that takes any iterable as an argument and sorts it according to the given key. If you already have a list and just want to sort it in place the .sort() method is probably quicker and clearer.

This function makes a copy of the list given in the argument, then returns ti sorted in ascending order. We can reverse the sort with,

new_list = sorted(my_list, reverse=True)

has no return value. It does not create a copy, but sorts in place. We can reverse sort the same as above.

Not a method.

Not a function.

1 Like

All right, thank you very much, clearer now :slight_smile:

1 Like

Absolutely! I learned, that I shouldn’t worry about these, yet, I will deeply understand them later. My learning technique is to get into many things simultaneously, and try to make basic connections fast, which I can improve in time. I studied physics, and I already have a vague idea about coding, so deeper explanations won’t frighten me :smiley:
Thanks very much! Will definitely look into those lessons you mentioned!

dear Roy
I wonder why this code returns two"None", but not the sorted list ? What specific knowledge do I miss?
Thank you very much in advance.

This first None comes from the return value of print(), the second from the return value of object.sort().

I don’t exactly understand, though. If you’re setting prints_return equal to a print statement that says ‘What does this print function return?’ then why does that sentence not print when you print prints_return?

Please post your code so we can point out the details.

I’m referring to the Default Returns section of the Python course. i’ll attach it below.

# store the result of this print() statement in the variable prints_return
print("What does this print function return?")
prints_return = print("What does this print function return?")

# print out the value of prints_return

# call sort_this_list.sort() and save that in list_sort_return
sort_this_list = [14, 631, 4, 51358, 50000000]
list_sort_return = sort_this_list.sort()
# print out the value of list_sort_return

That should print None. Can you explain why?

That should print None. Can you explain why?

I can’t. I know that it has something to do with the print statement that is assigned to prints_return, because, for example, if you had the following code:

prints_return = 'A random sentence I came up with.'

then the sentence would print.

Recall that print() is a function. What is the default return when a function has no return statement?

None. But why doesn’t print have a return function?

It has no return statement because there is nothing to return. It’s directing output to the display.

Ok. But in functions where we return something, for example a function that returns a product of two numbers, that product also is displayed. What’s it doing if not directing output to the display? Sorry if I’m asking too many questions.

Only if we print the return value. return is not the same as print, not even remotely.

def mul(a, b):
    return a * b

print (mul(6, 7))    # print the call expression

Above we print the return value from the function.

1 Like

Some interactive python interpreters will output the evaluation of the last expression (repr or str representation) for the sake of debugging but don’t confuse that with something that occurs normally. It definitely won’t happen when running scripts and shouldn’t be relied upon for anything.

1 Like