- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we return multiple values in Python?
It is possible to return multiple values from a function in the form of tuple, list, dictionary or an object of a user defined class
Return as tuple
>>> def function(): a=10; b=10 return a,b >>> x=function() >>> type(x) <class 'tuple'> >>> x (10, 10) >>> x,y=function() >>> x,y (10, 10)
Return as list
>>> def function(): a=10; b=10 return [a,b] >>> x=function() >>> x [10, 10] >>> type(x) <class 'list'>
Return as dictionary
>>> def function(): d=dict() a=10; b=10 d['a']=a; d['b']=b return d >>> x=function() >>> x {'a': 10, 'b': 10} >>> type(x) <class 'dict'>
Return as object of user defined class
>>> class tmp: def __init__(self, a,b): self.a=a self.b=b >>> def function(): a=10; b=10 t=tmp(a,b) return t >>> x=function() >>> type(x) <class '__main__.tmp'> >>> x.a 10 >>> x.b 10
- Related Articles
- How can we return multiple values from a function in C#?
- How can we return multiple values from a function in C/C++?
- How do we assign values to variables in Python?
- How can we do Python operator overloading with multiple operands?
- How do we evaluate a string and return an object in Python?
- How do we use file.readlines() to read multiple lines using Python?
- How to return multiple values to caller method in c#?
- Can a method return multiple values in Java?
- How to do multiple imports in Python?
- How do I return multiple results in a MySQL subquery with IN()?
- Returning Multiple Values in Python?
- How we can bundle multiple python modules?
- Find and return array positions of multiple values JavaScript
- How can we print multiple blank lines in python?
- How we can extend multiple Python classes in inheritance?

Advertisements