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:
Java Program to Implement the Program Used in grep/egrep/fgrep
Python List remove()
Python Operator Overloading
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String casefold()
Python Program to Convert Bytes to a String
Python Program to Access Index of a List Using for Loop
Python Set intersection_update()
Python Program to Slice Lists
Python Program to Safely Create a Nested Directory
Python __import__()
Python Program to Get the File Name From the File Path
Python Program to Check if a Key is Already Present in a Dictionary
Python pow()
Python delattr()
Python List clear()
Python next()
Python Set pop()
Python Program to Make a Simple Calculator
Python Program to Check If a String Is a Number (Float)
Python Program to Find the Largest Among Three Numbers
Python String rjust()
Python str()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Operators
Python Function Arguments
Python Set update()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Create Pyramid Patterns
Python int()
Python String rstrip()
Python Program to Find All File with .txt Extension Present Inside a Directory