Advertisements
Advertisements
प्रश्न
Using an example showing how a function in Python can return multiple values.
थोडक्यात उत्तर
Advertisements
उत्तर
(a) Either receive the returned values in the form of 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 the tuple by specifying the same number of variables on the left-hand side of the assignment in the 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 16shaalaa.com
Scope of a Variable in Python
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
