Python Concepts/Tuples

Objective

 * Learn about Python tuples.
 * Learn about tuple indexing.
 * Learn about tuple slicing.
 * Learn about built-in tuple functions.
 * Learn when to use tuples and when not to.

The Python Tuple
In Python, a tuple is an immutable sequence. This means that a tuple is similar to a list, except you cannot dynamically modify the tuple itself. Once it's created, it can't be changed or modified. The way the tuple is stored in memory will also be important later in the course. To create a tuple, you create a group of items separated by a comma. Even though Python adds parentheses around a group of items, it isn't required because it wouldn't change the meaning of the tuple.

Now critically think about this question. What if you need only one item stored in your tuple? You can accomplish this by leaving an excess comma at the end. In fact, you can leave an excess comma at the end of any tuple, regardless of size, although it isn't explicitly required for tuples larger than one. This is a little quirk in the Python Language.

Also note that an empty tuple is simply a pair of parentheses. To review:

Tuple Indexing
Tuples, like lists and strings, follow the same standard for indexing. Indexing starts at 0 for the first item and -1 for the start of the last item. Examples are shown below.

Tuple Slicing
Like other common sequences, tuples can be sliced. The standard slicing is exactly like slicing taught in previous lessons:

Tuples also support extended slicing. Again, the third parameter acts like a step:

Returns the number of times a given item is found.

Finds the first index of a given item. The two optional parameters are for the start and end of the tuple's index, respectively.

To determine the exact position of a value (including False if not found):

Tuple Usage
Tuples are great for holding static data that you don't plan on modifying and changing a lot. Like the previous lesson stated, tuples are four to seven times faster than lists. This means for every list item manipulated, four to seven tuples could be manipulated within that time frame. This has huge advantages for scientific work, as this allows for speedy data reading.

Although all of the tuple's advantages sound good, the tuple itself is static and immutable. This means that a tuple's content is read-only. To change its content, a tuple must be converted to a dynamic, mutable sequence and then converted back to a tuple. For this reason, if you do a lot of adding and removing of items in a sequence, you shouldn't use the tuple.

It is not possible to assign to the individual items of a tuple, however it is possible to create tuples that contain mutable objects, such as lists. Tuples containing lists behave like lists. For example, create a tuple containing a list of five rows in which each row contains four powers of integer $$i: [i,\ i^2,\ i^3,\ i^4]:$$

For a true deep copy:

Assignments

 * Work with some tuples. Try creating a tuple of tuples.
 * Work with some of the tuple's methods. Try them all out at least three times.
 * Tuples are the fastest sequence in Python. How could they be used in a real life situation?

Further Reading or Review

 * Previous Lesson: Bytes objects and Bytearrays
 * This lesson: Tuples
 * Next Lesson: Sets
 * Course Home Page