Harold Nelson
2/4/2022
A tuple is a sequence of values separated by commas. A tuple may or may not be enclosed in parentheses.
Here are some examples.
## (1, 2, 3)
## (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.
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
## 1
## 3
Problem 2: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.
The same concatenation and repetition we used with strings works with tuples.
Examples
## (1, 2, 3, 'a', 'b', 'c')
## (1, 2, 3, 1, 2, 3, 'a', 'b', 'c')
## (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.
Example
The following line of code will fail. Try it in idle or cocalc.
## 1
Problem 4: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.
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
## ([1, 2, 3], [4, 5, 6])
## ([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.
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
## (1, 2, 3)
## [1, 2, 3, 23]
## (1, 2, 3, 23)
Problem 6: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.
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
The contents of a tuple can be put in a list.
A list has a sort() method.
A list can be converted to a tuple.
Stop the video and work the problem!
## (4, 5, 11, 12)
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
## (1, 2, 3)
## 1
## 2
## 3
Problem 7: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.
After the following python code executes, what are the values of a, b, and c?
Stop the video and decide before you look.
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.
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
## 9
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
## False
## False
## True
Problem 8: Stop the Video and Doodle! Create your own simple exercise to demonstrate the contents of this slide.