# Return value of a print() in python

#1

I just completed my https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/multiple-return.I began to wonder what would be the return value of print().
When i run this piece of code,I got None as the answer.Can anyone explain why is it so?

b=print(“a”)
OUTPUT: None

#2

Every Python function for which there is no specified return value returns the value None. The function print() is no exception.

print() is a function which does something, but returns None. What it does is convert the object within its parentheses to a stream of text, then send that stream to a standard output (stdout), usually your console screen.

Your example looks like you meant:

b = print('a')
print(b)
# Output:
a
None


When the first line is executed, as with every assignment statement, the first thing that happens is that the expression to the right of the assignment operator (=) is executed, and its returned value is placed in memory. This is print('a'), and when it is executed, you see a appear on your screen.

Next, the memory address of the value returned by that expression is assigned to the variable on the left. The value is None and the variable is b, so when we get to the second line, print(b), the value of b is turned into a stream of text and sent to the screen, where we see the text None appear.

2 Likes
#3

Thank you very much for helping me out.
Is None similar to Void of C language??Can we consider a function say add_num(None) has no arguments??

#4

Well, we don’t need to type functions in Python, so there’s no equivalent to void f() In Python, you just write def f():, and if f() has no return statement, it returns None.

void (and Null) have some meanings in C with respect to pointers, but in Python, we don’t have pointers, so there’s no analogy there. Python doesn’t quake at the thought of empty memory addresses.

None is a value, and you can’t define a function with a value as a (positional) parameter.

def f(None):
pass

# Output:
Traceback (most recent call last):
File "C:\path\to\test.py", line 1
def f(None):
^
SyntaxError: invalid syntax


You’d get the same error with True, 23 or ‘abc’ as parameters.

You can pass None as an argument:

def f(a):
print(a)

f(None)

# Output
None