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
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.