Why would you assign the result of a function to a tuple (Intro to Python, Fundamental Functions: Intermediate, step 7)

I kind of did not understand the point of using a tuple to store the result of a function. Can someone explain the point of this?

That doesn’t look like a tuple to me.

a_sum and a_diff are just two variables that are being assigned values from the output of the executed function.

If I am not mistaken a tuple has a faster lookup speed and it has a static fixed order.Thus it cant be changed. A large list would have significant overhead compared to a large tuple.

Maybe this will help:

@jonas.roel Here it says the left hand side of sequence unpacking can only be list or tuple https://bugs.python.org/issue5018

It is assigning to a tuple because the function returned more than 1 value. Of course you can write 2 functions, 1 returning sum, 1 returning difference, and call them 1 by 1 and assign to a_sum and a_diff on two separate lines, but the author designed it as such for convenience. If the calculations of 2 results depended on the same inputs and process the inputs in a similar manner except for some steps, it could be better code organization to do both in one function and returning multiple values. However some would argue any function should do 1 thing only so it’s easily understandable and testable, then now the discussion becomes what is “1 thing” and how big this thing can get before it looks like 2 concepts.

Doing multiple assignment/sequence-unpacking could be useful when thinking about certain concepts, such as assigning to x,y = when describing a cartesian point, or when you are manipulating configurations for a matplotlib plot, where these “display control” variables abound.

You can also see such multiple assignment in gradient descent implementations, where all the theta’s should be updated simultaneously:
https://stackoverflow.com/questions/8725673/multiple-assignment-and-evaluation-order-in-python. (python’s name binding, shallow copy, deep copy is also relevant to this topic of assigning back to same variable)
Writing it in 1 line rather than line by line expresses clearly this mathematical implementation intention. (It does take some understanding of the programming language features to get it though).

If you haven’t heard yet, dynamic unpacking is really useful tool, it lets you handle unknown number of elements in right hand side so you don’t have to match it in left hand side (this is achieved by adding *args syntax): https://treyhunner.com/2018/03/tuple-unpacking-improves-python-code-readability/


@hanqi That is very informative. Thank you for the clarification.

Thanks for taking the time to explain it all. You seem to know a lot. I’m doing the Data Science set of courses. What’s the best way to understand the deep complexities of Python code besides the documentation? Would an introductory CS book cover the topic of “sequence unpacking”. I guess question is there anything that the DS courses don’t cover about Python that I should make sure I don’t skip? Any advice for a beginner would be much appreciated. Thanks again!

Thanks for the link. I’ll be adding to my notes for further reference!

Write enough wrong code and see enough errors, and nothing will be new. Or even if they are, you can google the error and read some stack overflows, get exposed to new keywords to google, read some GitHub threads, copy paste their code run it in your environment, create your own test set, tweak their answers, ask many questions, then you will never forget this lesson. (SAVE YOUR NOTES)

You can subscribe to “better developers” newsletter, and other short courses on https://lerner.co.il.
There are too many things to learn and I don’t think it’s effective to go through a whole book then see how to use the knowledge, but more effective to ride on your curiosity and start from tutorials online. Sources such as https://treyhunner.com and https://realpython.com are good starters, former writes in layman and goes in good detail, latter is comprehensive and practical. Even for books, I wouldn’t touch fundamental CS books yet, but go through practically explained books like https://realpython.com/products/python-tricks-book/ and https://www.goodreads.com/book/show/35917398-powerful-python.

I don’t think any DS course can cover everything about python. Just class design, function/operator overriding, test-driven development, are things that are relevant to doing well on a job, but I rarely see DS courses teach that as they are generic software engineering knowledge. So I would say the crucial success factor is your curiosity and passion, to immerse yourself in information from multiple sources and develop the grit to work through issues to the end. How about setting yourself a goal to contribute to pandas (https://www.codetriage.com), through reading other people’s comments, you learn how professionals think about design, what they care about, and realise how fragile everything is. Try to get a collective work experience (even open source works) as that’s where you really learn how to mess up in git and fix things.

Off the cuff, some always useful python knowledge are iterators/generators (so you save memory), set vs list (so you use O(1) rather than O(n) membership checks), data structure manipulation (dict/list merging), itertools + collections built-in libraries, and throwing generator functions/expressions into constructors like pd.DataFrame() and dict() and chaining generators (taught in DQ hacker news pipeline).
Binge on some pydata https://www.youtube.com/user/PyDataTV, they share decades of experience and bring you up from 0 to pro in an hr, and you can spend the next few days implementing it.


Thanks. I will have to pay this forward someday!