In this example, you will learn to return multiple values from a function.
To understand this example, you should have the knowledge of the following Python programming topics:
1. Example 1: Return values using comma
def name():
return "John","Armin"
# print the tuple with the returned values
print(name())
# get the individual items
name_1, name_2 = name()
print(name_1, name_2)
Output
('John', 'Armin')
John Armin
When you return multiple values using comma(s), they are returned in the form of a tuple. As shown in the code above, two strings "John" and "Armin" are returned with a single return statement.
2. Example 2: Using a dictionary
def name():
n1 = "John"
n2 = "Armin"
return {1:n1, 2:n2}
names = name()
print(names)
Output
{1: 'John', 2: 'Armin'}
When you return values using a dictionary, it is easy for you to keep track of the returned values using the keys. The return statement returns the two variables in the form a dictionary.
Related posts:
Python Set pop()
Python Program to Transpose a Matrix
Python Program to Merge Two Dictionaries
Python bytearray()
Python object()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python round()
Python Program to Create Pyramid Patterns
Python globals()
Python Modules
Python String format_map()
How to Get Started With Python?
Python dict()
Python Tuple count()
Python List append()
Python String format()
Python List remove()
Python Program to Get File Creation and Modification Date
Python Global, Local and Nonlocal variables
Python Program to Sort Words in Alphabetic Order
Python strftime()
Python String find()
Intelligent Projects Using Python - Santanu Pattanayak
Python Set union()
Python Program to Extract Extension From the File Name
Python Global Keyword
Python List Comprehension
Python String isprintable()
Python Program to Solve Quadratic Equation
Python String lstrip()
Python datetime
Python String zfill()