Return two values from a function in PythonSometimes 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.
Use ObjectIt 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. ExamplesThe 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. 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. Use TupleA 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. ExamplesThe 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. 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. Use listA 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. ExamplesThe 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. 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. Use DictionaryIn other languages, a dictionary is like a hash or a map. Here is more about the dictionary. ExamplesThe 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. Example2: Output The output values show two data using the python function. Using the Data ClassIf 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. ExamplesThe 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. 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. ConclusionThe 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. Next TopicComplete roadmap to learn Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India