Returning Multiple Values in Python
Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be imported easily and used to perform many different kinds of operations; to import these libraries or to use these libraries, we only need to install a package called python installation package (pip).
Python has considered an object-oriented programming language with code and data; the data in the object-oriented program is in terms of attributes and the code in terms of methods or functions. In the year 1989, Guido van Rossum was the one who introduced the python programming language.
It is widely used in machine learning, and data science departments deal with heavy data. It is also used in web applications, like the Django and Flask frameworks are created using python. It is a straightforward language to learn. Compared to other programming languages, the syntax in python is much easier. Many colleges and institutions have introduced python in their syllabus so that the students need to learn python. Python is a very user-friendly designed programming language. The syntax is so simple that a beginner can understand it very easily. In a python programming language, we can have any number of values for a function.
Returning multiple values for a function
In the python programming language, we can have multiple values for a function, similar to a class in C programming, C++ programming, and Java programming languages. Here class is created to have multiple values which return an object. Now let us look into a python program that returns multiple values using class.
Example
# python program, which returns multiple values using class
class exam:
def init(self):
self.str = “ ironman ”
self.y = 2
# now, the function returns an object of the exam
def func():
return exam()
# driver code testing the method
r = func()
print(r.str)
print(r.y)
Output
iron man
2
Returning multiple values from a function using a tuple
A tuple, which consists of items, is separated by a comma (, ). Types are created using parenthesis or without using parenthesis. Tuples are considered to be immutable, means, which cannot be changed once they are declared.
Now, let us look into a program that returns multiple values using a tuple
Example
# program in python which return multiple values using tuple
# creating a function that returns a tuple
def func( ) :
s = “ iron man.”
y = 10
return s, y ;
#return tuple this can also be returned as (s, y )
# driver code for testing the above method
s, x = func( )
# allocating the returned tuple
print( s )
print( y )
Output
iron man
10
Returning multiple values from a function using the list.
A list is considered an array that consists of items created with the help of square brackets ( [ ] ). They are different from arrays; arrays are considered to be having similar items in them, while a list has a different collection of items in them separated by commas (, ).
Lists are mutable, meaning you can change a list even after declaring it; these are considered different from tuples as tuples are immutable and are created using parenthesis, while lists are created using square brackets.
Now, let us look into a python program that returns multiple values using lists.
Example
# program in python which return multiple values using lists
# creating function which returns a list
def func( )
s = “ iron man”
y = 20
return [ s , y ] ;
# driver code for testing the above method
lst = func( )
print(lst)
Output
[ ‘iron man’, 20 ]
Returning multiple values from a function using a dictionary
Let us look into a program that returns multiple values using a dictionary
Example
# program in python which returns multiple values from a function using a dictionary
# creating a function that returns a dictionary
def func( )
d = dict( ) ;
d[ ‘s’ ] = “iron man”
d[ ‘y’ ] = 10
return d
# testing the driver code for the above method
d = func( )
print( d )
Output
{ ‘y’: 10, ‘s’: ‘iron man’ }