Q. Can a function return multiple values how?
Answer =
Yes.
(a)
Either receive the returned values in form a tuple variable.
For example: -
def squared(x, y, z):
return x, y *y, z * z
t = squared (2,3,4)
print(t)
Output is:-
Tuple t will be printed as:
(4, 9, 16)
(b)
Or you can directly unpack the received values of tuple by specifying the same number of variables on the left-hand side of the assignment in function call.
For example: -
def squared(x, y, z):
return x* x, y * y, z * z
v1, v2, v3 = squared (2,3,4)
print("The returned values are as under:")
print (v1, v2, v3)
Output is:-
4 9 16
Thanks .. Good Explanation
ReplyDeleteWelcome : )
DeleteBro....In first code 2nd line, in return statement. Check it.
ReplyDeleteWhat?? All are correct.
DeleteActually, there's slight little mistake that's in 2nd line. In return it should be x*x, y*y,z*z but instead it's x,y*y,z*z.... As per the output.
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )