Named tuples were added in 2.6 for this purpose. Also see os.stat for a similar builtin example.
>>>
import collections
>>>
Point = collections.namedtuple('Point', ['x', 'y']) >>>
p = Point(1, y = 2) >>>
p.x, p.y
1 2
>>>
p[0], p[1]
1 2
Example (From the docs):
class Employee(NamedTuple): # inherit from typing.NamedTuple
name: str
id: int = 3 #
default value
employee = Employee('Guido')
assert employee.id == 3
Returning a dictionary with keys "y0"
, "y1"
, "y2"
, etc. doesn't offer any advantage over tuples. Returning a ReturnValue
instance with properties .y0
, .y1
, .y2
, etc. doesn't offer any advantage over tuples either. You need to start naming things if you want to get anywhere, and you can do that using tuples anyway:
def get_image_data(filename): [snip]
return size, (format, version, compression), (width, height)
size, type, dimensions = get_image_data(x)
A lot of the answers suggest you need to return a collection of some sort, like a dictionary or a list. You could leave off the extra syntax and just write out the return values, comma-separated. Note: this technically returns a tuple.
def f():
return True, False
x, y = f()
print(x)
print(y)
gives:
True False
If your concerned about type look up, use descriptive dictionary keys, for example, 'x-values list'.
def g(x):
y0 = x + 1
y1 = x * 3
y2 = y0 ** y3
return {
'y0': y0,
'y1': y1,
'y2': y2
}
Another option would be using generators:
>>> def f(x):
y0 = x + 1
yield y0
yield x * 3
yield y0 ** 4
>>>
a, b, c = f(5) >>>
a
6
>>>
b
15
>>>
c
1296
I prefer:
def g(x):
y0 = x + 1
y1 = x * 3
y2 = y0 ** y3
return {
'y0': y0,
'y1': y1,
'y2': y2
}
Remote Work 2022 , Remote Work 2022
In this method, we use Python to return multiple values by simply separating them by commas. Python basically uses a tuple to achieve this.
#Returning Multiple Values using Tuples
def multiple():
operation = "Sum"
total = 5 + 10
return operation, total;
operation, total = multiple()
print(operation, total)
#Output = Sum 15
Since they are a collection of items we could add values to the list and return the list.
#Returning Multiple Values using List
def multiple():
operation = "Sum"
total = 5 + 10
return [operation, total];
values = multiple()
print(values)
#Output = ["Sum", 15]
In the previous method, accessing a particular value would be a hassle. However, this would not be a problem while using dictionaries to return multiple values. This is because we could use relevant key names and they could be accessed easily.
#Returning Multiple Values using Dictionary
def multiple():
values = dict();
values['operation'] = "Sum"
values['total'] = 5 + 10
return values;
values = multiple()
print(values)
#Output = {
'operation': 'Sum',
'total': 15
}
To return multiple values from a function, return the values as a tuple. To then access/store these values into variables, use the tuple destructuring.,To access/store the multiple values returned by a function, use tuple destructuring.,You can use tuple destructuring to access tuple values and store them into variables.,To return multiple values from a function in Python, return a tuple of values.
Here is how it looks in code:
def operate(a, b):
sum = a + b
diff = a - b
mul = a * b
div = a / b
return sum, diff, mul, div
Now you can call this function for two numbers and assign the return values to variables:
n1 = 5
n2 = 10
sum, diff, mul, div = operate(n1, n2)
print(
f "The sum is {sum}\n"
f "The difference is {diff}\n"
f "The multiplication gives {mul}\n"
f "The division gives {div}\n"
)
This results in the following being printed in the console:
The sum is 15 The difference is - 5 The multiplication gives 50 The division gives 0.5
For example, you can write the above 3D point as:
coords = 1, 1, 3
For example, let’s store the 3D points into variables x, y, and z:
coords = 1, 1, 3 x = coords[0] y = coords[1] z = coords[2] print(x, y, z)
Last Updated : 25 Nov, 2020
Output:
geeksforgeeks 20
Python functions can return multiple variables. These variables can be stored in variables directly. A function is not required to return a variable, it can return zero, one, two or more variables.,Create a function getPerson(). As you already know a function can return a single variable, but it can also return multiple variables.,In that case you can return variables from a function. In the most simple case you can return a single variable:,Variables defined in a function are only known in the function. That’s because of the scope of the variable. In general that’s not a problem, unless you want to use the function output in your program.
123
def complexfunction(a, b): sum = a + b
return sum
123456789101112
#!/usr/bin/env python3def getPerson(): name = "Leona" age = 35 country = "UK" return name,age,countryname,age,country = getPerson()print(name)print(age)print(country)
A function can return only one value or object as output, but if you want to return multiple values then you can return the same with the help of list, dictionary and tuple.,Return statement simply returns the values as output whereas print() function simply print the value.,In Python we use “return” as a keyword, here we can use a function with or without a return statement. If any function is called with return statement it simply returns the value and exit a function.,If you just want to execute a simple function that returns a value, the return statement will be enough. In case you want to return a value but also have to print it to the terminal, you need to use the print() method. The print() method can be easily used for displaying the result of a function call to the user.
Example With Return Statement
# Python 3 Code
# working on
return statement
def addvalue(a, b):
return a + b
c = addvalue(10, 34)
print(c)
Output
44
Example Without Return Statement
# Python 3 Code
# Function without
return statement
def addvalue(a, b):
# Print the value of a + b
print(a + b)
addvalue(10, 34)
Output:
Addition: 44 Subtraction: -24
# Python 3 Code
# Function
return Boolean True / False
def myfunction(a, b):
if (a > b):
return True # Return True
elif(a == b):
return 'A is Equal to B'
# Return String
else:
return False # Return False
# Check Boolean
print(myfunction(10, 34))
print(myfunction(10, 10))
print(myfunction(22, 11))
# Python 3 Code
# working on
return statement
def addvalue(a, b):
return a + b
c = addvalue(10, 34)
print(c)
def addvalue(a, b):
# Print the value of a + b
print(a + b)
addvalue(10, 34)