Function printing in Print Function in Python - python

My question is for below code why None is getting print in place of function output and why function output is getting before its position.
def print_spam():
print('spam')
def do_twice(r,ps):
g = ps()
print(r,'is a',g)
print(r,'is a',ps())
do_twice('xyz',print_spam)
Output is
spam
xyz is a None
spam
xyz is a None

The function print_spam() does not return anything. It just prints a statement.
Change it to:
def print_spam():
print('spam')
return 'spam'
Because your function doesn't return anything, it defaults to None. Now when assigning the function output to g, it will contain the returned string of the function (spam).

Consider replacing
g = ps()
print(r,'is a',g)
with
g = ps
print r,'is a',
g()
anyway, as pointed out by some other answers, print_spam() returns None

Related

Keep getting None after my expected result. Trying to get just the 'nth' item of the list printed [duplicate]

I tried writing this code:
def smaller(x, y):
if x > y:
print(y)
else:
print(x)
print(smaller(2, 3))
I got this result:
>>>
2
None
Where did the None come from? What does it mean?
See also
The accepted answer explains the importance of returning a value from the function, rather than printing it. For more information, see What is the purpose of the return statement? How is it different from printing?.
To understand the None result itself, see What is a 'NoneType' object?.
If you are printing inside the function in order to see multiple values, it may be better to instead collect those values so that they can be printed by the calling code. For details, see How can I use `return` to get back multiple values from a loop? Can I put them in a list?.
It's the return value of the function, which you print out. If there is no return statement (or just a return without an argument), an implicit return None is added to the end of a function.
You probably want to return the values in the function instead of printing them:
def jiskya(x, y):
if x > y:
return y
else:
return x
print(jiskya(2, 3))
Ok, to start off when you do this:
print(jiskya(2, 3))
You getting something pretty much equivalent to this:
print(print(2))
So, what is going on? The print(2) is printing out 2, and returns None which is printed by the outer call. Straightforward enough.
Now look at this:
def hello():
return 2
If you do:
print(hello())
You get 2 because if you print out a function you get whatever the return value is. (The return value is denoted by the return someVariable.
Now even though print doesn't have parenthesis like most functions, it is a function just a little special in that respect. What does print return? Nothing. So when you print print someVariable, you will get None as the second part because the return value of print is None.
So as others have stated:
def jiskya(x, y):
if x > y:
print(y)
else:
print(x)
Should be re-written:
def jiskya(x, y):
if x > y:
return y
else:
return x
Where did the 'None' come from?
The function.
And what is it?
It's what the function returned.
In Python, every function returns something. It could "be multiple things" using a tuple, or it could "be nothing" using None, but it must return something. This is how we deal with the fact that there is no way to specify a return type (which would make no sense since you don't specify types for anything else). When interpreted as a string for printing, None is replaced with the string "None".
None is a special object that is supposed to represent the absence of any real thing. Its type is NoneType (it is an instance of that class). Whenever you don't explicitly return anything, you implicitly return None.
You wrote the function to print one of the two values x or y, but not to return anything. So None was returned. Then you asked Python to print the result of calling the function. So it called the function (printing one of the values), then printed the return value, which was None, as the text "None".
You are doing two prints, the first one in the corpus of your function and the second one is printing the result of the function, which as actually None.
You should rather do something like this:
def yourfunction(x, y):
if x > y:
return y
else:
return x
Then,
>>> print yourfunction(2, 3)
2
Ya, basically you are using print statements in your function as a way to return information. You shouldn't do this. Print is NOT the same as a return statement. If you simply want your function to give your answer without the none, just type jiskya(2, 3) instead. You will see what the function throws out because you have print statements in the function. If instead you typed "return" in your function, it wouldn't give you anything without the "print" preceding the function call.
The problem is you wrote print jiskya(2,3). You're passing the return value of jiskya to the print function. jiskya itself prints x or y, which is why you see the 2. But then the print in the print jiskya(2, 3) statement itself executes with no argument.
To the interpreter, this is a simplification of what happens:
print jiskya(2,3)
>> Executing jiskya with arguments 2, 3
>> jiskya evaulates `print x`
>> 2 is printed
>> Function jiskya exits with no return value
print None
>> None is printed
Consider following examples:
Function without return statement
Print() function type is none type..
def test1():
print("code...!!!")
type(test1())
Output: code...!!!
NoneType
Function with return statement
Returning variable 'a' which holds print() function, that's why type() returns data type of print function which is NoneType, not the data type of what's inside the print funcion:
def test1():
a= print("code...!!!")
return a
type(test1())
Output: code...!!!
NoneType
Here function returning data type of variable 'a' which holds a string in it.
def test1():
a = "First code...!!!"
return a
type(test1())
Output: str

Why does my function return a value and None at the same time? [duplicate]

I tried writing this code:
def smaller(x, y):
if x > y:
print(y)
else:
print(x)
print(smaller(2, 3))
I got this result:
>>>
2
None
Where did the None come from? What does it mean?
See also
The accepted answer explains the importance of returning a value from the function, rather than printing it. For more information, see What is the purpose of the return statement? How is it different from printing?.
To understand the None result itself, see What is a 'NoneType' object?.
If you are printing inside the function in order to see multiple values, it may be better to instead collect those values so that they can be printed by the calling code. For details, see How can I use `return` to get back multiple values from a loop? Can I put them in a list?.
It's the return value of the function, which you print out. If there is no return statement (or just a return without an argument), an implicit return None is added to the end of a function.
You probably want to return the values in the function instead of printing them:
def jiskya(x, y):
if x > y:
return y
else:
return x
print(jiskya(2, 3))
Ok, to start off when you do this:
print(jiskya(2, 3))
You getting something pretty much equivalent to this:
print(print(2))
So, what is going on? The print(2) is printing out 2, and returns None which is printed by the outer call. Straightforward enough.
Now look at this:
def hello():
return 2
If you do:
print(hello())
You get 2 because if you print out a function you get whatever the return value is. (The return value is denoted by the return someVariable.
Now even though print doesn't have parenthesis like most functions, it is a function just a little special in that respect. What does print return? Nothing. So when you print print someVariable, you will get None as the second part because the return value of print is None.
So as others have stated:
def jiskya(x, y):
if x > y:
print(y)
else:
print(x)
Should be re-written:
def jiskya(x, y):
if x > y:
return y
else:
return x
Where did the 'None' come from?
The function.
And what is it?
It's what the function returned.
In Python, every function returns something. It could "be multiple things" using a tuple, or it could "be nothing" using None, but it must return something. This is how we deal with the fact that there is no way to specify a return type (which would make no sense since you don't specify types for anything else). When interpreted as a string for printing, None is replaced with the string "None".
None is a special object that is supposed to represent the absence of any real thing. Its type is NoneType (it is an instance of that class). Whenever you don't explicitly return anything, you implicitly return None.
You wrote the function to print one of the two values x or y, but not to return anything. So None was returned. Then you asked Python to print the result of calling the function. So it called the function (printing one of the values), then printed the return value, which was None, as the text "None".
You are doing two prints, the first one in the corpus of your function and the second one is printing the result of the function, which as actually None.
You should rather do something like this:
def yourfunction(x, y):
if x > y:
return y
else:
return x
Then,
>>> print yourfunction(2, 3)
2
Ya, basically you are using print statements in your function as a way to return information. You shouldn't do this. Print is NOT the same as a return statement. If you simply want your function to give your answer without the none, just type jiskya(2, 3) instead. You will see what the function throws out because you have print statements in the function. If instead you typed "return" in your function, it wouldn't give you anything without the "print" preceding the function call.
The problem is you wrote print jiskya(2,3). You're passing the return value of jiskya to the print function. jiskya itself prints x or y, which is why you see the 2. But then the print in the print jiskya(2, 3) statement itself executes with no argument.
To the interpreter, this is a simplification of what happens:
print jiskya(2,3)
>> Executing jiskya with arguments 2, 3
>> jiskya evaulates `print x`
>> 2 is printed
>> Function jiskya exits with no return value
print None
>> None is printed
Consider following examples:
Function without return statement
Print() function type is none type..
def test1():
print("code...!!!")
type(test1())
Output: code...!!!
NoneType
Function with return statement
Returning variable 'a' which holds print() function, that's why type() returns data type of print function which is NoneType, not the data type of what's inside the print funcion:
def test1():
a= print("code...!!!")
return a
type(test1())
Output: code...!!!
NoneType
Here function returning data type of variable 'a' which holds a string in it.
def test1():
a = "First code...!!!"
return a
type(test1())
Output: str

What to do to remove None [duplicate]

I tried writing this code:
def smaller(x, y):
if x > y:
print(y)
else:
print(x)
print(smaller(2, 3))
I got this result:
>>>
2
None
Where did the None come from? What does it mean?
See also
The accepted answer explains the importance of returning a value from the function, rather than printing it. For more information, see What is the purpose of the return statement? How is it different from printing?.
To understand the None result itself, see What is a 'NoneType' object?.
If you are printing inside the function in order to see multiple values, it may be better to instead collect those values so that they can be printed by the calling code. For details, see How can I use `return` to get back multiple values from a loop? Can I put them in a list?.
It's the return value of the function, which you print out. If there is no return statement (or just a return without an argument), an implicit return None is added to the end of a function.
You probably want to return the values in the function instead of printing them:
def jiskya(x, y):
if x > y:
return y
else:
return x
print(jiskya(2, 3))
Ok, to start off when you do this:
print(jiskya(2, 3))
You getting something pretty much equivalent to this:
print(print(2))
So, what is going on? The print(2) is printing out 2, and returns None which is printed by the outer call. Straightforward enough.
Now look at this:
def hello():
return 2
If you do:
print(hello())
You get 2 because if you print out a function you get whatever the return value is. (The return value is denoted by the return someVariable.
Now even though print doesn't have parenthesis like most functions, it is a function just a little special in that respect. What does print return? Nothing. So when you print print someVariable, you will get None as the second part because the return value of print is None.
So as others have stated:
def jiskya(x, y):
if x > y:
print(y)
else:
print(x)
Should be re-written:
def jiskya(x, y):
if x > y:
return y
else:
return x
Where did the 'None' come from?
The function.
And what is it?
It's what the function returned.
In Python, every function returns something. It could "be multiple things" using a tuple, or it could "be nothing" using None, but it must return something. This is how we deal with the fact that there is no way to specify a return type (which would make no sense since you don't specify types for anything else). When interpreted as a string for printing, None is replaced with the string "None".
None is a special object that is supposed to represent the absence of any real thing. Its type is NoneType (it is an instance of that class). Whenever you don't explicitly return anything, you implicitly return None.
You wrote the function to print one of the two values x or y, but not to return anything. So None was returned. Then you asked Python to print the result of calling the function. So it called the function (printing one of the values), then printed the return value, which was None, as the text "None".
You are doing two prints, the first one in the corpus of your function and the second one is printing the result of the function, which as actually None.
You should rather do something like this:
def yourfunction(x, y):
if x > y:
return y
else:
return x
Then,
>>> print yourfunction(2, 3)
2
Ya, basically you are using print statements in your function as a way to return information. You shouldn't do this. Print is NOT the same as a return statement. If you simply want your function to give your answer without the none, just type jiskya(2, 3) instead. You will see what the function throws out because you have print statements in the function. If instead you typed "return" in your function, it wouldn't give you anything without the "print" preceding the function call.
The problem is you wrote print jiskya(2,3). You're passing the return value of jiskya to the print function. jiskya itself prints x or y, which is why you see the 2. But then the print in the print jiskya(2, 3) statement itself executes with no argument.
To the interpreter, this is a simplification of what happens:
print jiskya(2,3)
>> Executing jiskya with arguments 2, 3
>> jiskya evaulates `print x`
>> 2 is printed
>> Function jiskya exits with no return value
print None
>> None is printed
Consider following examples:
Function without return statement
Print() function type is none type..
def test1():
print("code...!!!")
type(test1())
Output: code...!!!
NoneType
Function with return statement
Returning variable 'a' which holds print() function, that's why type() returns data type of print function which is NoneType, not the data type of what's inside the print funcion:
def test1():
a= print("code...!!!")
return a
type(test1())
Output: code...!!!
NoneType
Here function returning data type of variable 'a' which holds a string in it.
def test1():
a = "First code...!!!"
return a
type(test1())
Output: str

python adding 'none' to the end of function being printed? [duplicate]

I tried writing this code:
def smaller(x, y):
if x > y:
print(y)
else:
print(x)
print(smaller(2, 3))
I got this result:
>>>
2
None
Where did the None come from? What does it mean?
See also
The accepted answer explains the importance of returning a value from the function, rather than printing it. For more information, see What is the purpose of the return statement? How is it different from printing?.
To understand the None result itself, see What is a 'NoneType' object?.
If you are printing inside the function in order to see multiple values, it may be better to instead collect those values so that they can be printed by the calling code. For details, see How can I use `return` to get back multiple values from a loop? Can I put them in a list?.
It's the return value of the function, which you print out. If there is no return statement (or just a return without an argument), an implicit return None is added to the end of a function.
You probably want to return the values in the function instead of printing them:
def jiskya(x, y):
if x > y:
return y
else:
return x
print(jiskya(2, 3))
Ok, to start off when you do this:
print(jiskya(2, 3))
You getting something pretty much equivalent to this:
print(print(2))
So, what is going on? The print(2) is printing out 2, and returns None which is printed by the outer call. Straightforward enough.
Now look at this:
def hello():
return 2
If you do:
print(hello())
You get 2 because if you print out a function you get whatever the return value is. (The return value is denoted by the return someVariable.
Now even though print doesn't have parenthesis like most functions, it is a function just a little special in that respect. What does print return? Nothing. So when you print print someVariable, you will get None as the second part because the return value of print is None.
So as others have stated:
def jiskya(x, y):
if x > y:
print(y)
else:
print(x)
Should be re-written:
def jiskya(x, y):
if x > y:
return y
else:
return x
Where did the 'None' come from?
The function.
And what is it?
It's what the function returned.
In Python, every function returns something. It could "be multiple things" using a tuple, or it could "be nothing" using None, but it must return something. This is how we deal with the fact that there is no way to specify a return type (which would make no sense since you don't specify types for anything else). When interpreted as a string for printing, None is replaced with the string "None".
None is a special object that is supposed to represent the absence of any real thing. Its type is NoneType (it is an instance of that class). Whenever you don't explicitly return anything, you implicitly return None.
You wrote the function to print one of the two values x or y, but not to return anything. So None was returned. Then you asked Python to print the result of calling the function. So it called the function (printing one of the values), then printed the return value, which was None, as the text "None".
You are doing two prints, the first one in the corpus of your function and the second one is printing the result of the function, which as actually None.
You should rather do something like this:
def yourfunction(x, y):
if x > y:
return y
else:
return x
Then,
>>> print yourfunction(2, 3)
2
Ya, basically you are using print statements in your function as a way to return information. You shouldn't do this. Print is NOT the same as a return statement. If you simply want your function to give your answer without the none, just type jiskya(2, 3) instead. You will see what the function throws out because you have print statements in the function. If instead you typed "return" in your function, it wouldn't give you anything without the "print" preceding the function call.
The problem is you wrote print jiskya(2,3). You're passing the return value of jiskya to the print function. jiskya itself prints x or y, which is why you see the 2. But then the print in the print jiskya(2, 3) statement itself executes with no argument.
To the interpreter, this is a simplification of what happens:
print jiskya(2,3)
>> Executing jiskya with arguments 2, 3
>> jiskya evaulates `print x`
>> 2 is printed
>> Function jiskya exits with no return value
print None
>> None is printed
Consider following examples:
Function without return statement
Print() function type is none type..
def test1():
print("code...!!!")
type(test1())
Output: code...!!!
NoneType
Function with return statement
Returning variable 'a' which holds print() function, that's why type() returns data type of print function which is NoneType, not the data type of what's inside the print funcion:
def test1():
a= print("code...!!!")
return a
type(test1())
Output: code...!!!
NoneType
Here function returning data type of variable 'a' which holds a string in it.
def test1():
a = "First code...!!!"
return a
type(test1())
Output: str

Why is "None" printed after my function's output?

I tried writing this code:
def smaller(x, y):
if x > y:
print(y)
else:
print(x)
print(smaller(2, 3))
I got this result:
>>>
2
None
Where did the None come from? What does it mean?
See also
The accepted answer explains the importance of returning a value from the function, rather than printing it. For more information, see What is the purpose of the return statement? How is it different from printing?.
To understand the None result itself, see What is a 'NoneType' object?.
If you are printing inside the function in order to see multiple values, it may be better to instead collect those values so that they can be printed by the calling code. For details, see How can I use `return` to get back multiple values from a loop? Can I put them in a list?.
It's the return value of the function, which you print out. If there is no return statement (or just a return without an argument), an implicit return None is added to the end of a function.
You probably want to return the values in the function instead of printing them:
def jiskya(x, y):
if x > y:
return y
else:
return x
print(jiskya(2, 3))
Ok, to start off when you do this:
print(jiskya(2, 3))
You getting something pretty much equivalent to this:
print(print(2))
So, what is going on? The print(2) is printing out 2, and returns None which is printed by the outer call. Straightforward enough.
Now look at this:
def hello():
return 2
If you do:
print(hello())
You get 2 because if you print out a function you get whatever the return value is. (The return value is denoted by the return someVariable.
Now even though print doesn't have parenthesis like most functions, it is a function just a little special in that respect. What does print return? Nothing. So when you print print someVariable, you will get None as the second part because the return value of print is None.
So as others have stated:
def jiskya(x, y):
if x > y:
print(y)
else:
print(x)
Should be re-written:
def jiskya(x, y):
if x > y:
return y
else:
return x
Where did the 'None' come from?
The function.
And what is it?
It's what the function returned.
In Python, every function returns something. It could "be multiple things" using a tuple, or it could "be nothing" using None, but it must return something. This is how we deal with the fact that there is no way to specify a return type (which would make no sense since you don't specify types for anything else). When interpreted as a string for printing, None is replaced with the string "None".
None is a special object that is supposed to represent the absence of any real thing. Its type is NoneType (it is an instance of that class). Whenever you don't explicitly return anything, you implicitly return None.
You wrote the function to print one of the two values x or y, but not to return anything. So None was returned. Then you asked Python to print the result of calling the function. So it called the function (printing one of the values), then printed the return value, which was None, as the text "None".
You are doing two prints, the first one in the corpus of your function and the second one is printing the result of the function, which as actually None.
You should rather do something like this:
def yourfunction(x, y):
if x > y:
return y
else:
return x
Then,
>>> print yourfunction(2, 3)
2
Ya, basically you are using print statements in your function as a way to return information. You shouldn't do this. Print is NOT the same as a return statement. If you simply want your function to give your answer without the none, just type jiskya(2, 3) instead. You will see what the function throws out because you have print statements in the function. If instead you typed "return" in your function, it wouldn't give you anything without the "print" preceding the function call.
The problem is you wrote print jiskya(2,3). You're passing the return value of jiskya to the print function. jiskya itself prints x or y, which is why you see the 2. But then the print in the print jiskya(2, 3) statement itself executes with no argument.
To the interpreter, this is a simplification of what happens:
print jiskya(2,3)
>> Executing jiskya with arguments 2, 3
>> jiskya evaulates `print x`
>> 2 is printed
>> Function jiskya exits with no return value
print None
>> None is printed
Consider following examples:
Function without return statement
Print() function type is none type..
def test1():
print("code...!!!")
type(test1())
Output: code...!!!
NoneType
Function with return statement
Returning variable 'a' which holds print() function, that's why type() returns data type of print function which is NoneType, not the data type of what's inside the print funcion:
def test1():
a= print("code...!!!")
return a
type(test1())
Output: code...!!!
NoneType
Here function returning data type of variable 'a' which holds a string in it.
def test1():
a = "First code...!!!"
return a
type(test1())
Output: str

Categories

Resources