Exercises on Tuples

Harold Nelson

2/4/2022

Tuples

A tuple is a sequence of values separated by commas. A tuple may or may not be enclosed in parentheses.

Here are some examples.

t1 = 1,2,3
t2 = (1,2,3)

print(t1)
## (1, 2, 3)
print(t2)
## (1, 2, 3)

Note that in both cases print() enclosed the tuples in parentheses.

Problem 1: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.

Extracting by Index

Values of tuple components may be extracted by numerical index in the same way as lists or strings. Slicing also works in exactly the same way.

Examples

x = t1[0]
print(x)
## 1
z = t1[-1]
print(z)
## 3

Problem 2: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.

Combining Tuples

The same concatenation and repetition we used with strings works with tuples.

Examples

t3 = ('a','b','c')
print(t1+t3)
## (1, 2, 3, 'a', 'b', 'c')
print(2*t1+t3)
## (1, 2, 3, 1, 2, 3, 'a', 'b', 'c')
print(2*(t1+t3))
## (1, 2, 3, 'a', 'b', 'c', 1, 2, 3, 'a', 'b', 'c')

Problem 3: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.

Tuples are Immutable

Example

The following line of code will fail. Try it in idle or cocalc.

# t1[0] = 4
print(t1[0])
## 1

Problem 4: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.

Note on Immutability

A tuple may contain items of any type. If an item in a tuple is mutable. The mutable item may be changed and this will be reflected in the tuple.

Example

List1 = [1,2,3]
List2 = [4,5,6]
tt = (List1,List2)
print(tt)
## ([1, 2, 3], [4, 5, 6])
List1.append('a')
print(tt)
## ([1, 2, 3, 'a'], [4, 5, 6])

Problem 5: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.

Conversion

A tuple may be converted to another data structure containing the same data. Lists have many methods that can alter them. The function tuple() can convert a list to a tuple.

Example

print(t1)
## (1, 2, 3)
L1 = list(t1)
L1.append(23)
print(L1)
## [1, 2, 3, 23]
t1 = tuple(L1)
print(t1)
## (1, 2, 3, 23)

Problem 6: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.

Exercise

Create a function tsort(t). It’s single parameter is a tuple. It is a fruitful function which returns the input tuple in sorted order. Test it with ta = (5,4,12,11).

Take advantage of these facts

  1. The contents of a tuple can be put in a list.

  2. A list has a sort() method.

  3. A list can be converted to a tuple.

Stop the video and work the problem!

Answer

def tsort(t):

    l = list(t)
    l.sort()
    tout = tuple(l)
    return(tout)
    
ta = (5,4,12,11)

print(tsort(ta))
## (4, 5, 11, 12)

Multiple Assignment

Multiple assignment is a valuable coding technique which allows us to extract the values in a tuple and assign them to specific variables. There must be exacly the same number of variables as values in the tuple.

Example

t1 = (1,2,3)
print(t1)
## (1, 2, 3)
First, Second, Third = t1
print(First)
## 1
print(Second)
## 2
print(Third)
## 3

Problem 7: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.

Swapping

After the following python code executes, what are the values of a, b, and c?

a = 1
b = 2
c = 3

a,b,c = c,a,b

Stop the video and decide before you look.

Answer

print(a,b,c)
## 3 1 2

Multiple Assignment (Complex)

You may skip this problem.

Write a python function to compute both the least common divisor and the greatest common divisor of two integers. There are two parameters, the two integers, which should be positive.

The function should return a tuple containing the least common divisor and the greatest common divisor in that order.

Stop the video and solve the problem.

Answer

def findExtremeDivisors(n1, n2):
    minVal, maxVal = None, None

    for i in range(2, min(n1, n2) + 1): 
        
        # Test for common divisor
        if n1%i == 0 and n2%i == 0: 
        
            if minVal == None or i < minVal: 
            
                minVal = i 
                
            if maxVal == None or i > maxVal: 
            
                maxVal = i 
    
    return (minVal, maxVal)

lcd, gcd = findExtremeDivisors(27,18)

print(lcd)
## 3
print(gcd)
## 9

Comparison

Tuples may be compared. Equality requires that both tuples have the same values in the same places. Comparison starts on the left and continues until a tie-breaker is found.

Examples

ta = (1,3,2)
tb = (1,2,3)

print(ta == tb)
## False
print(ta < tb)
## False
print(ta > tb)
## True

Problem 8: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.