Variables are a part of every programming language. But few languages handle variables as neatly and cleanly as Python. Users can typically manipulate variables with a wide variety of different methods and functions. And the results will usually be returned neatly and in a way that seldom leads to errors. However, you might wonder how you can go about returning multiple values from within your Python code. Thankfully, the language handles this in just as flexible and easy-to-understand manner as the rest of its lexicon.
Passing Values and Variables
Python’s syntax makes it extremely easy to pass values from a function. In fact, you really don’t have to do anything special to your variables before returning them at the end of a function. One returned variable is essentially the same as two, three, or more.
However, that doesn’t mean it’s always going to be that simple. Simplicity is the default when returning a variable. But the language’s versatility means that we can use variables in a number of creative ways to get some powerful functionality.
Different Options To Work With Multiple Values
We can begin by looking at a basic example, before moving on to the more advanced usage scenarios. Consider the following code.
def ourFunc(ourInt):
newList = [ourInt*2,ourInt*3,ourInt*5]
return newList
x = ourFunc(5)
print(str(x) + ” is a ” + str(type(x)) + ” and the first value is an ” + str(type(x[0])))
We begin by creating a function called ourFunc. It takes an integer and uses that value as a seed for a new three-value list. We can then pass back those multiple values by using Python’s return statement.
On line 5 we call ourFunc and assign the result to x. We move on to a print statement that highlights a few critical points. The print statement shows the contents of x. In this case, those values are 10, 15, and 25. The print statement goes on to show x’s class type. We can see that x is a list. And, finally, the print statement shows that the first item in that list is an integer.
Now try changing line 2 to the following.
newList = (ourInt*2,ourInt*3,ourInt*5)
This change highlights the versatility of Python’s syntax. The code runs just as well with this change. And that’s despite the fact that we’ve made a big modification to the underlying data. With the change, newList actually isn’t a list at all. Changing the bracket type switches us from a list definition to a tuple definition. But the data is returned in the same manner as it was before that alternation. And when we print out our information we can see that it is, in fact, a tuple now.
We’d of course need to change the code a little to work with a dictionary or other collections that use a key system. But Python’s syntax will generally let you return multiple items without much concern over how you go about it. In fact, it’ll even create a collection for us if we don’t formally define one. Using the previous example, change line 3 to the following.
return 5,6,7
The function no longer returns the data from newList. The ourFunc will now just return 5,6, and 7. The fact that the three numbers fit into the existing code is impressive enough in and of itself. But note the change in our print statement’s output. The x variable is still listed as a tuple. This is despite the fact that we haven’t actually defined the three numbers as a collection at all. Python’s interpreter saw that we were passing a collection of values that weren’t being altered. And it also knows that there’s a set definition for that type of collection – a tuple. Note that Python automatically provides some functionality similar to a specialize object or template in other languages.
Taking Variables in Different Directions
But what if we wanted to take things even further and work with a new class? For example, if we wanted to use a data class to store data? Take a look at the following code.
class ourClass:
def __init__(self):
self.ourList1 = [1,2,3,4,5]
self.ourList2 = [6,7,8,9,10]
def ourFunc():
return ourClass()
x = ourFunc()
print(x.ourList1)
print(x.ourList2)
print( type(x))
print( type(x.ourList1[1]))
We begin by creating a new class as ourClass. The constructor creates two lists of integers. Note that while we’re using integers, we could also use a mixed type list or other data structure options. The most important point is that the code remains easy to understand and easy to read. If the type of returned data becomes unpredictable then it’s also become somewhat risky for long-term use. Complicated code can become difficult to use and maintain over time. It’s best to keep things as organized as possible. And in this case, we’re demonstrating that by keeping the variables as a collection of integers.
We go on to create a function, ourFunc, which returns an instance of ourClass. With the class and function declarations finished we can move on to actually calling ourFunc and passing the result to x. We then print out some information about x. We begin by showing the variables in the lists initiated with ourClass. Note that ourFunc, as an instance of ourClass, automatically has all of the values in the two lists assigned to it. Finally, we show that the lists are just standard collections by printing out the type of x and an individual item from within it.
All of these options are a useful way to return multiple values. Simply passing the values as a returned collection is by far the easiest. But creating a full class will open the way to easier reuse if you’ll be using those values fairly often.