Tuples

The final built-in data type is the tuple. Python tuples work exactly like Python lists except they are immutable, i.e. they can't be changed in place. They are normally written inside parentheses to distinguish them from lists (which use square brackets), but as you'll see, parentheses aren't always necessary. Since tuples are immutable, their length is fixed. To grow or shrink a tuple, a new tuple must be created.
Here's the list of common operations for tuples:
 • () An empty tuple
 • t1 = (0, ) A one-item tuple (not an expression)
 • t2 = (0, 1, 2, 3) A four-item tuple
 • t3 = 0, 1, 2, 3 Another four-item tuple (same as prior line)
 • t3 = ('abc', ('def', 'ghi')) Nested tuples
 • t1[n], t3[n][j] Index
 • t1[i:j], slice
 • len(tl) length
 • t1 + t2 Concatenate
 • t2 * 3 repeat
 • for × in t2, Iteration
 • 3 in t2 membership

The second entry shows how to create a one item tuple. Since parentheses can surround expressions, you have to show Python when a single item is actually a tuple by placing a comma after the item. The fourth entry shows a tuple without parentheses; this form can be used when a tuple is unambiguous. However, it's easiest to just use parentheses than to figure out when they're optional.


Why Use Tuples?
Tuples typically store heterogeneous data, similar to how lists typically hold homogenous data. It’s not a hard-coded rule but simply a convention that some Python programmers follow. Because tuples are immutable, they can be used to store different data about a certain thing. For example, a contact list could conceivably stored within a tuple; you could have a name and address (both strings) plus a phone number (integer) within on data object.
The biggest thing to remember is that standard operations like slice and iteration return new tuple objects. In my opinion, I like use lists for everything except when I don't want a collection to change. It cuts down on the number of collections to think about, plus tuples don’t let you add new items to them or delete data. You have to make a new tuple in those cases. 
There are a few times when you simply have to use a tuple because your code requires it. However, a lot of times you never know exactly what you’re going to do with your code and having the flexibility of lists can be useful.
So why use tuples? Apart from sometimes being the only way to make your code work, there are few other reasons to use tuples:
 • Tuples are processed faster than lists. If you are creating a constant set of values that won’t change, and you need to simply iterate through them, use a tuple.
 • The sequences within a tuple are essentially protected from modification. This way, you won’t accidentally change the values, nor can someone misuse an API to modify the data. (An API is an application programming interface. It allows programmers to use a program without having to know the details of the whole program.)
 • Tuples can be used as keys for dictionaries. Honestly, I don’t think I’ve ever used this, nor can I think of a time when you would need to. But, it’s there if you ever need to use it.
 • Tuples are used in string formatting, by holding multiple values to be inserted into a string. In case you don’t remember, here’s a quick example:

  >>>val1 = “integer”
  >>>val2 = 2
  >>>”The %s value is equal to %d” % (val1, val2)

  ‘The integer value is equal to 2’


Sequence Unpacking
So, to create a tuple, we treat it like a list (just remembering to change the brackets).
>>>tuple = (1, 2, 3, 4)
The term for this is packing a tuple, because the data is “packed into” the tuple, all wrapped up and ready to go. So, to remove items from a tuple you simply unpack it. 

>>>first, second, third, fourth = tuple
>>> first
1
>>> second
2
>>> third
3
>>> fourth
4

Neat, huh? One benefit of tuple packing/unpacking is that you can swap items in-place. With other languages, you have to create the logic to do swap variables; with tuples, the logic is inherent in the data type.

>>> bug = "weevil"
>>> bird = "African swallow"
>>> bug, bird = bird, bug
>>> bug 
'African swallow'
>>> bird

'weevil'


Methods

Tuples have no methods. Sorry. 

The best you can do with tuples is slicing, iteration, packing and unpacking. However, Python has a neat little trick if you need more flexibility with tuples: you can change them into lists. Simply use the list() function call on a tuple and it magically becomes a list. Contrarily, you can call tuple() on a list and it becomes a tuple.

>>> my_list = ["moose", "Sweden", "llama"]
>>> my_tuple = ("Norwegian Blue", "parrot", "pet shop")
>>> tuple(my_list)
('moose', 'Sweden', 'llama')
>>> list(my_tuple)
['Norwegian Blue', 'parrot', 'pet shop']

Obviously the benefit to this is that you can arbitrarily switch between the two, depending on what you need to do. If, halfway through your program, you realize that you need to be able to manipulate a tuple but you don’t want it to be always modifiable, you can make a new variable that calls the list() function on the tuple and then use the new list as needed.
So, now that we have the fundamental building blocks down, we can move on to how you use them in practice. However, we’ll cover one last essential tool that all programmers need to know how to use: files.