There are multiple ways you can return multiple values from a function in Python. Below are some examples of returning multiple values from a function in Python –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
## Using a tuple def f(x): y0 = x + 1 y1 = x - 3 y2 = x * 2 return (y0, y1, y2) print(f(10)) ## Returns (11, 7, 20) ## Using a dictionary def f(x): y0 = x + 1 y1 = x - 3 y2 = x * 2 return {"y0": y0,"y1": y1,"y2": y2} print(f(10)) ## Returns {'y0': 11, 'y1': 7, 'y2': 20} ## Using a list def f(x): y0 = x + 1 y1 = x - 3 y2 = x * 2 return [y0,y1,y2] print(f(10)) ## Returns [11, 7, 20] ## Using a class class ReturnValue: def __init__(self, y0, y1, y2): self.y0 = y0 self.y1 = y1 self.y2 = y2 def g(x): y0 = x + 1 y1 = x - 3 y2 = x * 2 return ReturnValue(y0, y1, y2) print(g(10).y1) ## Returns 7 |