Javatpoint Logo
Javatpoint Logo

Return two values from a function in Python

Sometimes web application requires displaying two values in a function using Python language. It is necessary for complicated functionality, mathematical operations, and the display of database information. There are multiple ways provided by Python to return two values.

  • Using object
  • Using Tuple
  • Using list
  • Using data class
  • Using dictionary

Use Object

It is like C++ as well as Java language that we can make a class that can hold two or more than one value. The values are returned as an object of that class.

Examples

The following examples return two similar and different data types values. The Python language uses an object method in a function.

Example1: the following example shows two numbers as an output using the python function. Here, we use integer values to return as an output.

Output

The output values show two data using the python function.

Return two values from a function in Python

Example2: the following example shows two values as an output using the python function. Here, we use integer and string values as input data.

Output

The output values show two data using the python function.

Return two values from a function in Python

Use Tuple

A tuple shows values that are separated by commas. It is made whether or not with () sign. In Python, a tuple value can't be changed after providing it as input.

Examples

The following examples return two similar and different data types values. The python language uses the tuple method in a function.

Example1: the following example shows two numbers as an output using the python function.

Output

The output values show two data using the python function.

Return two values from a function in Python

Example2: the following example shows two values as an output using the python function with one string and one integer value.

Output

The output values show two data using the python function.

Return two values from a function in Python

Use list

A list is a group of items that are put together with square brackets. They're different from arrays because they can hold different kinds of items. Lists are not the same as tuples because they can be changed.

Examples

The following examples return two similar and different data types values using the list method in Python.

Example1: the following example shows two numbers as an output using the python function.

Output

The output values show two data using the python function.

Return two values from a function in Python

Example2: the following example shows two values as an output using the python function.

Output

The output values show two data using the python function.

Return two values from a function in Python

Use Dictionary

In other languages, a dictionary is like a hash or a map. Here is more about the dictionary.

Examples

The following examples return two similar and different data types values using the dictionary method in Python.

Example1:

Output

The output values show two data using the python function.

Return two values from a function in Python

Example2:

Output

The output values show two data using the python function.

Return two values from a function in Python

Using the Data Class

If you are using Python 3.7 or later, you can use the Data Class to return a class that has automatically added unique methods. The Data Class module has a decorator and functions that automatically add special methods like __init_() & __repr_() to user-defined classes.

Examples

The following examples return two similar and different data types values using data class in Python.

Example1: the following example shows two numbers as an output using the python function. Here, we use the same data type's values.

Output

The output values show two data using the python function.

Return two values from a function in Python

Example2: the following example shows two values as an output using the python function. Here, we use two different data types' values.

Output

The output values show two data using the python function.

Return two values from a function in Python

Conclusion

The python language provides functions and different methods to return two or more than one value. The method uses convenience, requirement, and input value. It displays output in minimum Python coding for the developer and displays output easily to the user.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA