FAQ: Learn Python: Functions - On Beyond Strings

This community-built FAQ covers the “On Beyond Strings” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise On Beyond Strings:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

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

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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

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

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Why did we use the print as well as return command in this example ?

Same question here! It seems to be no difference using it or not.


Why is return needed here?

It would be the norm, not the exception in most cases. Here the author wishes the functions to print the output, rather than print the call returns, but normally we would print on return, not inside the function.

return is important if we wish to keep the data alive when we exit the function. It is the only means we have of handing the data back to the calling scope.

def distance_from_zero(arg):
    return abs(arg)

print (distance_from_zero(-10))

or if we wish to perform futher computation or evaluation on the return, we assign it to a variable so it can be polled again.

dist_from_zero = distance_from_zero(-10)
print (dist_from_zero)    # 10
2 Likes

why do the first two functions have a * before their argument?

It tells the interpreter to expect one or more comma-separated arguments. In Python it is known as the splat operator.

>>> def foo (*args):
    a = args
    for x in a:
        print (x)

        
>>> foo('bar')
bar
>>> foo(1,2,3,4,5)
1
2
3
4
5
>>> 
2 Likes
def biggest_number(*args):
    print(max(args))
    return max(args)

def smallest_number(*args):
    print(min(args))
    return min(args)

def distance_from_zero(arg):
    print(abs(arg))
    return abs(arg)

biggest_number(-10, -5, 5, 10)
smallest_number(-10, 5, 10)
distance_from_zero(-10)

In this example, you can see that with the 2 functions: (biggest_number and smallest_number), the parameters of both have an asterisk before it whereas distance_from_zero does not. This is for one simple reason: If you want to have more than one or multiple numbers defining a function, you need to put an asterisk before it’s parameter in order for the syntax to support more than 1 number. Because if you look at the bottom of the script, you realize that both biggest_number and smallest_number have been defined with more than 1 number, whereas distance_from _zero is defined only with -10.

DO NOT FORGET TO PUT AN ASTERISK BEFORE YOUR PARAMETER IF THE FUNCTION IS DEFINED WITH TWO OR MORE NUMBERS. IT COULD MEAN THE DIFFERENCE BETWEEN A SYNTAX ERROR AND A FULLY FUNCTIONAL CODE