How to return multiple values in Python
井民全, Jing, mqjing@gmail.com
Back to the Main Page
Google doc: This document
Purpose
This document descripts how to return multiples values for a function in Python.
Tuple
Code
def g(x):
y0 = x + 1
y1 = x * 3
y2 = y0 ** 3
return (y0, y1, y2)
|
Usage
retTuple = g(1)
print(retTuple)
y0, y1, y2 = g(1)
print('y0=', y0, 'y1=',y1, 'y2=',y2)
|
Dictionary
Code
def g(x):
y0 = x + 1
y1 = x * 3
y2 = y0 ** 3
return {'y0':y0, 'y1':y1 ,'y2':y2 }
|
Usage
retDict = g(1)
print(retDict)
print(retDict['y0'])
|
Result
{'y0': 2, 'y1': 3, 'y2': 8}
2