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
 

Keep Current with matt harrison

matt harrison Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @__mharrison__

1 Jan
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). ⚒🧠
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!