×

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’ }

Related Topics

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

1 minute read.

Best books for NLP with Python

NLP NLP – Natural language processing Computers were created to make the life of mankind easier. But computers cannot understand human language. Instead, they interact with numbers to perform the tasks allotted...

12 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Python File Handling

What is the file? The file is a collection of data in a single unit. It is used to store information in the non-volatile memory such as hard-disk. Computer programs generally run on the RAM...

7 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

Python pynmea2

Define Pynmea2 Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in...

4 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of...

4 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

Python Program to Check Leap Year

Python Program to Check Leap Year Leap year: We all know that each year has 365 or 366 days. But whenever a year has 366 days, then the year is said...

2 minutes read.