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.
π 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(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.
print(yi)
1 2.7 3800.0 4.9
Another way to unpack the tuple is by assigning a new variable for each element.
print(y0,y1,y2,y3)
1 2.7 3800.0 4.9
π Tuple Length
Get the length of the tuple with the len() function.
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.
print(i,y[i])
0 1 1 2.7 2 3800.0
Print every other element of the tuple.
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.
(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.
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.
1
β° Maximum, Minimum, Sum
Get the maximum max(y), minimum min(y), and summation (sum(y)) values of a tuple or list.
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.
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.
print(type(w))
π» Exercise 2B
Create a new tuple with values from rolling a 6-sided die π² 10 times:
Count and display the number of times each value appears.
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.
- Correct. Python is index-0 based. Thus, y[2] accesses the third element which is 3800.0.
- Incorrect. y[3] accesses the fourth element which is 4.9.
- Incorrect. Square brackets are used for indexing in Python, not parentheses.
- 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)?
- Correct. The slice y[1:3] includes the elements at indices 1 and 2, but not the one at index 3.
- Incorrect. The slice y[1:3] does not include the element at index 3.
- Incorrect. The slice y[1:3] starts from the second element, not the first.
- Incorrect. The slice y[1:3] includes the element at index 2.