How To Return A Tuple In Python? (With 7 Examples)

How To Return A Tuple In Python

To return a tuple in Python, you can use the parenthesis operators.

These parentheses are not necessary.

But it is always recommended to use parentheses.

Let’s see one by one how you can return a tuple in Python.

In the end, you will see some solved examples to grasp the concept even more stronger.

Method 1

Return a tuple in Python

Here is how you can return a tuple in Python:

def my_func():
    value1 = 1;
    value2 = 2;
    value3 = 3;
    return (value1, value2, value3)
    
values = my_func()
print(values)

Output

(1, 2, 3)

In the above example, my_func() returns a tuple of value1, value2, and value3.

The parentheses with the return statement are optional.

You can omit these parentheses and the code will work exactly the same.

def my_func():
    value1 = 1;
    value2 = 2;
    value3 = 3;
    return value1, value2, value3
    
values = my_func()
print(values)

Output

(1, 2, 3)

You can return a tuple from a function using this method.

Now let’s see some solved examples to better understand this.

These solved example programs will start from the easiest ones.

And as we progress, you will see relatively tough problems that you will enjoy.

Example 1

Program to return a tuple of mixed data types

Let’s see how you can return a tuple of mixed data types in Python.

def mixed_data_types():
    return ('John', 25, 3.14, True)

print(mixed_data_types())  # Output: ('John', 25, 3.14, True)

Output

(‘John’, 25, 3.14, True)

You can return mixed data types as a tuple.

In the above example, we returned a string, integer, float, and even a boolean.

You can extend this according to your requirements.

Exmaple 2

Python program to return a tuple of two lists

Now let’s see how you can return a tuple of two lists in Python.

def two_lists():
    list1 = [1, 2, 3]
    list2 = ['a', 'b', 'c']
    return (list1, list2)

print(two_lists())

Output

([1, 2, 3], [‘a’, ‘b’, ‘c’])

In the above example, we returned two lists as a tuple.

For the sake of simplicity, we defined the lists within the function.

You can take input from the user or any way you want.

Similarly, you can return two sets as a tuple:

def two_sets():
    set1 = {1, 2, 3}
    set2 = {'a', 'b', 'c'}
    return (set1, set2)

print(two_sets())

Output

({1, 2, 3}, {‘a’, ‘c’, ‘b’})

That was some basics.

Now let’s move on to some real-life and useful examples.

Example 3

Get two numbers from user and return these numbers as a tuple

The first example is pretty simple and easy.

You have to write a function that prompts the user to enter two numbers.

These two numbers are then returned as a tuple.

Give it a try before going to the solution using our Online Python Compiler.

Tried it? Very good.

Now, let’s see the solution.

def get_two_numbers():
    number1 = int(input("Enter the first number: "))
    number2 = int(input("Enter the second number: "))
    return (number1, number2)

numbers = get_two_numbers()
print(numbers)

Output

Enter the first number: 3
Enter the second number: 4
(3, 4)

When we called the function get_two_numbers() it prompts the user to input two numbers.

The user entered 3 and 4.

Both these numbers were returned as a tuple.

Example 4

Get name and age of the user and return as a tuple

In the second example, you have to write a function.

A function that prompts the user to enter name and age.

Then return the name and age as a tuple.

Give it a try before going to the solution using our Online Python Compiler.

Let’s see what the program.

def get_name_and_age():
    name = input("Enter you name: ")
    age = int(input("Enter your age: "))
    return (name, age)

data = get_name_and_age()
print(data)

Output

Enter you name: Allen
Enter your age: 19
(‘Allen’, 19)

We created a function get_name_and_age()

This function prompts the user to enter the name and age.

Then, the name and age were returned as a tuple.

When we called the function get_name_and_age() the returned tuple was stored in a data variable.

After that, we print it.

Example 5

Program to return the minimum and maximum values of a list

In this example, you have to create a function that takes a list as an argument.

Then you have to find the maximum and minimum values of the list.

Finally, you have to return these maximum and minimum values as a tuple.

Give it a try before going to the solution using our Online Python Compiler.

Here is the program:

def min_and_max(my_list):
    minimum = min(my_list)
    maximum = max(my_list)
    return (minimum, maximum)

my_list = [3, 5, 2, 8, 1]
print(min_and_max(my_list))

Output

(1, 8)

We created a function min_and_max()

This function takes a list as an argument.

Then, we used the min() and max() functions to find the minimum and maximum values of the list.

Finally, we returned the minimum and the maximum values as a tuple.

Example 6

Program to return the length and sum of a list

In this program, you have to create a function.

This function should take a list as an argument.

Then, you have to find the length and the sum of the list.

Give it a try before going to the solution using our Online Python Compiler.

Here is the code:

def length_and_sum(my_list):
    length = len(my_list)
    total = sum(my_list)
    return (length, total)

my_list = [3, 5, 2, 8, 1]
print(length_and_sum(my_list)) 

Output

(5, 19)

The function length_and_sum() takes a list as an argument.

Inside the function, we find the length using the len() function and the sum using the sum() function.

Finally, we returned both values as a tuple.

Example 7

Calculate diameter, circumference and area of circle from radius

In this example, you have to create a function.

A function that takes the radius of the function as an argument.

Then calculate the diameter, circumference, and area of the circle.

Finally, it should return all three values as a tuple.

Give it a try before going to the solution using our Online Python Compiler.

Let’s see the program.

import math

def calculate_circle_stats(radius):
    diameter = 2 * radius
    circumference = 2 * math.pi * radius
    area = math.pi * radius**2
    return (diameter, circumference, area)


calculations = calculate_circle_stats(3)
print(calculations)

Output

(6, 18.84955592153876, 28.27433388230814)


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading