Python Program to Return Multiple Values From a Function – 2024

Hey guys, in this blog we will see a Python Program to Return Multiple Values From a Function.

Table of Contents

Example 1: Return values using a comma

# Python Program to Return Multiple Values From a Function

def name():
    return "Abhishek","Sharma"

# print the tuple with the returned values
print(name())

# get the individual items
name_1, name_2 = name()
print(name_1, name_2)

Output

('Abhishek', 'Sharma')
Abhishek Sharma
  • Here we have printed our returned values in two ways.
  • In the first way, we have not unpacked our values hence a tuple is printed.
  • Whereas in a second way we have unpacked the values in two variables name1, and name 2, and printed them.

Example 2: Using a dictionary

def name():
    n1 = "Abhishek"
    n2 = "Sharma"

    return {1:n1, 2:n2}

names = name()
print(names)

Output

{1: 'Abhishek', 2: 'Sharma'}
  • We can also pass a dictionary from a function containing multiple values and we can further unpack it according to our needs.

Check out our other python programming examples

Leave a Reply

Your email address will not be published. Required fields are marked *