Unpacking arguments in Python
If you have used Python even for a few days now, you probably know about unpacking tuples. Well for starter, you can unpack tuples or lists to separate variables but that not it. There is a lot more to unpack in Python.
Unpacking without storing the values: You might encounter a situation where you might not need all the values from a tuple but you want to store only some of them. In that case, you can use an _ to ignore certain values. Now, let’s combine it with the above * implementation
love ['Geeks ', 'for ', 'Geeks ']
Notice here the first and last variables are set to underscore ( _ ). In python, underscore is used for ignoring values or throw away variables, which means that “I ” and 3000 won’t be stored.
I love 3000
Notice here in a and b, the first and second values get assigned and in c, the last value gets assigned. So, what about the third, fourth, and fifth? Well, they simply get ignored because we have used *_. If we used * with a variable then all those would get into that variable as a list. Since, we have used _ instead of a variable, so the entire list of words gets ignored completely.
Example 3: Well, the idea here is to create a function that will take in a list of numbers and return its sum, average, maximum, and minimum in the list. We will then reuse the function to get that we need for different use cases.
CASE 1 88 12.571428571428571 50 1 CASE 2 12.571428571428571 50 CASE 3 88 1
The above code is for demonstrating how you can have one function returning multiple values but use only the ones necessary at a time without wasting memory.