Program to Return Multiple Values Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

A return keyword in Python is used to end the execution of a function even if it is not the last statement. It cannot be used outside any method. A python function is not restricted to returning a single variable. It can return null or one or more values as per requirement. Python has a default property to return multiple values or variables.

Return Multiple Values Python by Different Methods

In Python, functions can return single or multiple values or null. To return multiple values python functions use the following methods:

  • An Object
  • A Tuple (comma-separated values)
  • A list
  • A Dictionary
  • Data Class

Method 1: Using Object

Classes are user-defined blueprints that help us create an “object”. Objects in Python are the instances of a particular class. In the following example, the reference of an object is returned using the return statement in the powers() method i.e return Test().

Code

Output

Method 2: Using Tuple

To return multiple values python mostly uses a tuple. A tuple in Python is a collection of elements and is immutable. It contains a sequence of items separated by a comma. Tuples may or may not be enclosed within parenthesis ().

Code

Output

Method 3: Using a List

A list in python is a mutable collection of different types of data. The comma (,) separates the elements in the list, which are enclosed in square brackets []. Like tuples, lists can also be used to return multiple values from a function.

Code

Output

Method 4: Using a Dictionary

Dictionary in Python is an important data structure that stores data in key-value pair format. The keys are immutable, and must always be unique as they help in locating data in the memory. The values can be of any data type and may or may not be unique.

Code

Output

Method 5: Using Set

A set in Python is an unordered collection of items where every element is unique and immutable. The comma (,) separates the elements in the set, which are enclosed in curly brackets {}. Like tuples and lists, sets can also return multiple values from a function. To return a set, at first the set object ans_set is created within the function body, and then returned to the caller of the function using the keyword operation return ans_set.

Code

Output

FAQs

Q: Which is the best method to return multiple values from a function?

A: The most suitable method varies from program to program, depending on its requirement.

Q: Which method helps users to keep track of the returned variables easily?**

A: To return multiple values python dictionary method makes work easier by finding and keeping track of the returned variables.

Conclusion

  • The return in Python is used to terminate the flow of execution of the particular method.
  • This statement can return null, one, or multiple values per requirement.
  • To return multiple values python functions can use an object, a tuple, a list, a dictionary, or a set.