Python/Tuples

< Python

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.

Lesson

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 on in the course. To create a tuple, you need to create a group of items separated by a comma (,). Even though Python adds parentheses around group of items, it isn't required since it wouldn't change the meaning of the tuple at all.

>>> spam = 1, 2, 3
>>> spam
(1, 2, 3)
>>> "A", "B", "C", "D"
('A', 'B', 'C', 'D')
>>> True, False, True, True, False
(True, False, True, True, False)
>>> (1, 3, "G", True)
(1, 3, 'G', True)


Now critically think about this question. What if you only need one item stored in your tuple? You can simply 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.

>>> bacon = 1,
>>> bacon
(1,)
>>> spam = 1, 2, 3,
>>> spam
(1, 2, 3)

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

Tuple Indexing

Tuples, like lists and strings, follows the same standard for indexing. Indexing starts at 0 for the first item and -1 for the start of the last item. A brief example is shown below.

>>> ("A", "B", "C", "D", "E", "F")[0]
'A'
>>> ("A", "B", "C", "D", "E", "F")[1]
'B'
>>> ("A", "B", "C", "D", "E", "F")[-1]
'F'
>>> ("A", "B", "C", "D", "E", "F")[-2]
'E'

Tuple Slicing

Like other common sequences, tuples can be sliced. The standard slicing is exactly like slicing taught in previous lessons. A quick example is given below.

>>> ("A", "B", "C", "D", "E", "F")[1:]
('B', 'C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[:4]
('A', 'B', 'C', 'D')
>>> ("A", "B", "C", "D", "E", "F")[-4:]
('C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[0:]
('A', 'B', 'C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[0:2]
('A', 'B')
>>> ("A", "B", "C", "D", "E", "F")[11:]
()
>>> ("A", "B", "C", "D", "E", "F")[:-19]
()


Tuples also support extend slicing. Again, the third parameter acts like a step. An example is given below.

>>> ("A", "B", "C", "D", "E", "F")[::]
('A', 'B', 'C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[::1]
('A', 'B', 'C', 'D', 'E', 'F')
>>> ("A", "B", "C", "D", "E", "F")[::2]
('A', 'C', 'E')
>>> ("A", "B", "C", "D", "E", "F")[::3]
('A', 'D')
>>> ("A", "B", "C", "D", "E", "F")[::-1]
('F', 'E', 'D', 'C', 'B', 'A')
>>> ("A", "B", "C", "D", "E", "F")[::-2]
('F', 'D', 'B')
>>> ("A", "B", "C", "D", "E", "F")[:3:-1]
('F', 'E')
>>> ("A", "B", "C", "D", "E", "F")[0:4:1]
('A', 'B', 'C', 'D')
>>> ("A", "B", "C", "D", "E", "F")[-2::-1]
('E', 'D', 'C', 'B', 'A')
>>> ("A", "B", "C", "D", "E", "F")[:-19:-1]
('F', 'E', 'D', 'C', 'B', 'A')
>>> ("A", "B", "C", "D", "E", "F")[::11]
('A',)
>>> ("A", "B", "C", "D", "E", "F")[::-56]
('F',)


Note: Extended slicing can be tricky for some people. If you aren't to confident with them, you should go back and review this lesson. This lesson also has some general information about common sequence tricks.

.count()

Returns the number of times a given item is found.

>>> votes = ("yes", "no", "yes", "yes", "no")
>>> votes.count("yes")
3
>>> votes.count("no")
2


.index()

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

>>> votes = ("yes", "no", "yes", "yes", "no")
>>> vote.index("no")
1
>>> vote.index("yes")
0
>>> vote.index("yes", 1)
2
>>> vote.index("no", 2)
4
>>> vote.index("yes", 1, 4)
2


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 tuples 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.

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?

Completion status: Ready for testing by learners and teachers. Please begin!

This article is issued from Wikiversity - version of the Wednesday, August 26, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.