2️⃣ πŸ“— Python Tuple

A tuple is one of four types that can store multiple items as a single variable. A tuple is a finite set of values and is designed to be static, much like a constant that is defined once and does not change.

πŸ’‘ Create Tuple

Create a tuple of numbers. A tuple of length 1 is defined with a comma (x,). At least one comma is needed to define a tuple.

y = (1,2.7,3.8e3,4.9)

πŸ“ Print Tuple

Use the function tuple() to convert a set or list to a tuple. Once it is a tuple type, it is not designed to be changed, but y can be redefined as a new tuple.

print(y)
print(type(y))
    (1, 2.7, 3800.0, 4.9)
    <class 'tuple'>

πŸ“‘ Unpack Tuple

Each element of a tuple is accessed when used as an iterator in a for loop.

for yi in y:
    print(yi)
    1
    2.7
    3800.0
    4.9

Another way to unpack the tuple is by assigning a new variable for each element.

y0,y1,y2,y3 = y
print(y0,y1,y2,y3)
    1 2.7 3800.0 4.9

πŸ“ Tuple Length

Get the length of the tuple with the len() function.

len(y)
    4

πŸ—„ Tuple Index

Even though the tuple is defined with parenthesis y = (i,x,e,float(s)), references to elements of the tuple are with square brackets y[0]. Python is index-0 so the first element is y[0], the second element is y[1], and so on.

    1

The last element is y[3] (for this tuple) or y[-1] (for any tuple). The second-to-last element is y[-2], third-to-last element is y[-3], and so on.

    4.9

πŸ“‘ Iterate with range

A common way to cycle through the elements is with range to loop a certain number of times. Print the first 3 elements of the tuple.

for i in range(3):
    print(i,y[i])
    0 1
    1 2.7
    2 3800.0

Print every other element of the tuple.

for i in range(0,len(y),2):
    print(i,y[i])
    0 1
    2 3800.0

πŸ”ͺ Tuple Slice

A tuple slice is a subset of the full tuple. It is defined as a range of values such as y[1:3] where the last number in the range 3 is not included in the slice.

y[1:3]
    (2.7, 3800.0)

Leaving out the final number y[1:], directs the slice to start at the second index 1 and finish at the end of the tuple.

    (2.7, 3800.0, 4.9)

Leaving out the first number y[:3], directs the slice to start the beginning.

    (1, 2.7, 3800.0)

πŸ—ƒ Get Index of Value

The index of each tuple element is available with the index() function that is included with each tuple object. If there are multiple 2.7 values in the tuple, the index of the first occurance is returned.

y.index(2.7)
    1

πŸ“Š Count Number of Values

Unlike a set, a tuple can have multiple of the same value. The number of each occurance is available with the count() function. In this case, there is only one 3800 value.

y.count(3800)
    1

β›° Maximum, Minimum, Sum

Get the maximum max(y), minimum min(y), and summation (sum(y)) values of a tuple or list.

print(max(y),min(y),sum(y))
    3800.0 1 3808.6

πŸ”‘Tuple Attributes and Methods with dir

Use the dir() function to list all attributes (constants, properties) and methods (functions) that are available with each object.

dir(y)

The tuple has the methods (functions):

  • index - get the index of the first element instance
  • count - count the number of specific elements

πŸ’» Exercise 2A

Create a tuple w with a single value 2.7. Verify that it is a tuple with print(type(w)) to show <class 'tuple'>. See Create Tuple section for information on creating tuple with a single value.

w = (2.7,)
print(type(w))

πŸ’» Exercise 2B

Create a new tuple with values from rolling a 6-sided die 🎲 10 times:

(6,2,2,3,5,2,4,6,1,3)

Count and display the number of times each value appears.

d = (6,2,2,3,5,2,4,6,1,3)
for i in range(1,7):
   print(i,d.count(i)))

βœ… Knowledge Check

1. What is the syntax to access the third element in the tuple y = (1, 2.7, 3800.0, 4.9). Select 'two correct answers.

A. y[2]
Correct. Python is index-0 based. Thus, y[2] accesses the third element which is 3800.0.
B. y[3]
Incorrect. y[3] accesses the fourth element which is 4.9.
C. y(2)
Incorrect. Square brackets are used for indexing in Python, not parentheses.
D. y[-2]
Correct. Negative indexing starts from the end. y[-2] accesses the second-to-last element as 3800.0.

2. What is the output of the tuple slice operation y[1:3] on the tuple y = (1, 2.7, 3800.0, 4.9)?

A. (2.7, 3800.0)
Correct. The slice y[1:3] includes the elements at indices 1 and 2, but not the one at index 3.
B. (2.7, 3800.0, 4.9)
Incorrect. The slice y[1:3] does not include the element at index 3.
C. (1, 2.7, 3800.0)
Incorrect. The slice y[1:3] starts from the second element, not the first.
D. (1, 2.7)
Incorrect. The slice y[1:3] includes the element at index 2.
πŸ’¬