-1

I was doing some experimenting while going through Coursera courses on functions and variables.

def x():
  print(20)
y = x
y()
print(y())

The results I got back were:

20
20
None

Why is there an extra None being printed? What is the logic behind this?

Shane Low
  • 3
  • 4
  • 1
    That None has nothing to do with assigning functions to variables. You would have seen the same effect if you did `print(x())` and removed `y` from your code entirely. – user2357112 Nov 14 '20 at 04:53
  • Simply put, because your function returns `None` and you print the result of calling your function. – juanpa.arrivillaga Nov 14 '20 at 06:24

1 Answers1

0

It is because the return value of x is None. If you omit return at the end of function definition, the function returns None. And print at the bottom prints the value returned from x.

JUSEOK KO
  • 69
  • 7