Trouble With Return Statements

Shouldn’t the code below print : result: [ 2 ,4, 6 ] in charcoal ??
I’m having trouble understanding where return statements and functions return to in python ?
When they say return statements and functions return to the ’ calling code’, that means
to the calling function outside of the actual function ? No one ever explains this very well.
However, I do understand that if you assign the function to a variable, the function will
return the result to this variable.

def get_even(numbers):
    even_nums = [num for num in numbers if not num % 2]
    return even_nums
get_even([1, 2, 3, 4, 5, 6])

Also can someone tell me why when I hit ‘post’ my formatting goes away ?

You’re probably not seeing the result if you select all 4 lines and hit enter. If you just change the last line to print(get_even([1, 2, 3, 4, 5, 6])) it should print out.

Or, just do it once (so the function is defined) then select only the last line and execute. Maya only auto-prints single line statements

That is it ! thank You !!

Thanks again for that info.
My main question is :

For a function without a return statement, the result is returned to the function that called it ?

Whats the difference between that and the return statement ?

return really means “leave my current function” so you can use it purely for flow control:

def return_nothing():
      for x in range (100):
           print (x)
           if x  > 49:

This one will always break after printing up through 50. If you change it like this…

def return_50():
      for x in range (100):
           print (x)
           if x  > 49:
               return x

… the behavior of the function itself is the same, but in the outer context you will get back the number 50:

def test():

      nothing = return_nothing()
      number = return_50()

# None
# 50

The first example doesn’t return anything, which Python treats there same as return None. The second returns the number 50. In the Maya listener when you evaluate a single line (like return_50() ) it automatically prints the result, but that’s just a convenience for use while developing – in anything serious you have functions calling other functions and using the results.

Thanks for all the examples !