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 print()
Python Statement, Indentation and Comments
Python Program to Calculate the Area of a Triangle
Python Program to Get the File Name From the File Path
Python List remove()
Python super()
Python Tuple
Python String isdecimal()
Python Set difference()
Python Program to Find Armstrong Number in an Interval
Python type()
Serverless Functions with Spring Cloud Function
Python pow()
Python String islower()
Python Dictionary pop()
Python String encode()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Objects and Classes
Python Program to Merge Two Dictionaries
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Generators
Python Dictionary fromkeys()
Python iter()
Python Exception Handling Using try, except and finally statement
Python Program to Copy a File
Python property()
Python Shallow Copy and Deep Copy
Python Set isdisjoint()
Python Program to Display the multiplication Table
Debug a JavaMail Program
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python ord()