×

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

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

Python Loop through a Dictionary

Introduction in this tutorial, we will discuss in python How to Loop Through a Dictionary. In contrast to other Data Types, which can only retain a single value as an element, a Dictionary...

4 minutes read.

Python input() function

Python input() function The input() function in Python reads a line from input and converts it to a string (stripping a trailing newline). Syntax input([prompt]) Parameter prompt: This function represents a string, depicting a default...

1 minute read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Python String Methods

Python String Methods Python has a set of built-in string methods. The Python string methods are as follows: capitalize() The string.capitalize() method returns a copy of the string by converting the...

5 minutes read.

Curdir Python

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 a...

3 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Python elif

Python elif The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them. Syntax if expression1: statement elif expression2: statement elif expression3: statement else: statement The elif statement can be optional...

3 minutes read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Python datetime

Python provides a module named datetime to work with the date and time. Sometimes in the real life application development, we need to work with the date and time. The date is...

3 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

Traverse Dictionary in Python

Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary In Python, dictionaries are a useful and commonly used data structure in...

3 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Periodogram in Python

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.