I'm teaching a Fundamentals of Python class this week.
We talked about tuples, and there was some confusion as to their existence.
Certainly, you could live in a world without them, but...
🧵
First of all ... pronunciation.
You can say "two-pull" or "tuh-pull" (both are valid).
A big confusion is when to use a list or tuple?
My basic rule of thumb:
If you have items of the same type and care about the order or re-arranging it, you want a list.
If you have a sequence of items with different types (then you wouldn't be re-arranging it), and the order is important, then tuple.
Tuples can be used for:
🐍 Record-type data
🐍 Returning multiple items from a function
🐍 *Unpacking* in assignment or for loops
🐍 Hashing sequences
🐍 Slots
Some examples from the standard library:
You can create them with a literal or the constructor. (There is also the unpack operator, but I find this hard to grok).
One annoying thing with pulling things out of tuples, is that you often introduce "magic numbers". If that is a problem, consider a named tuple (there are 2 ways to create them):
Thanks for reading 🙏
When do you use tuples?
If you liked this thread, please share and follow me for more 🐍💪
• • •
Missing some Tweet in this thread? You can try to
force a refresh
One of the most effective things I did for my career is probably the most difficult.
🧵👇
It is not difficult because it requires strength or knowledge, it is difficult because it requires will power.
I'm talking about writing a book. 📚
Writing a book taught me a bunch of lessons about work, myself, and business. I would say it jumpstarted my career (even though I had been employed for over 13 years by that point). ⚒🧠