⊕
⊖
Contents
Python Return Tuple
Like any other object, you can return a tuple from a function.
In this tutorial, we will learn how to return a tuple from a function in Python.
Examples
1. Write a function that returns a Tuple
In this example, we shall write a function that just returns a tuple, and does nothing else.
Python Program
def myFunction():
return (1, 'Ram')
tuple1 = myFunction()
print(tuple1)
print(type(tuple1))
Run Code Copy2. Return a Tuple formed using arguments to the function
In this example, we shall write a function that accepts arguments and returns a tuple formed with these arguments.
Python Program
def myFunction(rollno, name):
#create tuple
tempTuple = (rollno, name)
#return tuple
return tempTuple
tuple1 = myFunction(1, 'Mike')
print(tuple1)
print(type(tuple1))
Run Code CopyOutput
(1, 'Mike')
<class 'tuple'>
3. Return multiple Tuples from a function
In this example, we shall return multiple tuples from the function using generators.
Python Program
def myFunction(rollnos, names):
#create tuple
yield (rollnos[0], names[0])
yield (rollnos[1], names[1])
tuple1, tuple2 = myFunction([1, 2], ['Mike', 'Ram'])
print(tuple1)
print(tuple2)
Run Code CopyOutput
(1, 'Mike')
(2, 'Ram')
Summary
In this tutorial of Python Examples, we learned how to return Tuple from a function, with the help of well detailed example programs.