Q. Using an example show how a function in Python can return multiple values.
Answer:-
(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) 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
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )