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 repr()
Python complex()
Python List append()
Python Program to Access Index of a List Using for Loop
Python reversed()
Python String isupper()
Python Program to Display the multiplication Table
Python Program to Display Fibonacci Sequence Using Recursion
Python Dictionary pop()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Dictionary
Python String strip()
Python String replace()
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Convert Bytes to a String
Python Program to Remove Punctuations From a String
Python String partition()
Python Program to Get a Substring of a String
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Swap Two Variables
Python int()
Python Input, Output and Import
APIs in Node.js vs Python - A Comparison
Python Program to Merge Mails
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python frozenset()
Python Shallow Copy and Deep Copy
Python String isnumeric()
Python String rstrip()
Python Set discard()
Python String find()