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

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more