Tuples: 'Physics' 'Chemistry' "A" "B" "C" "D" "E"
Tuples: 'Physics' 'Chemistry' "A" "B" "C" "D" "E"
TUPLES
Introduction
A tuple is a sequence of immutable Python objects. The values stored in a tuple can be any type and they
are indexed by integers. Tuples are comparabe and hashable so we can sort lists of them and use tuples as
key values in python dictionaries.
• The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use
parentheses, whereas lists use square brackets.
• Optionally, you can put these comma-separated values between parentheses also.
#creation of tuples
tup1=('physics','chemistry',1997,2000)
tup2=(1,2,3,4,5)
tup3="a","b","c","d","e"
print(tup1)
print(tup2)
print(tup3)
In [7]:
#to create a tuple with a single element, comma should be included in the end
t1=('x')
print(type(t1)) #string
t2=('y',)
print(type(t2)) #tuple
<class 'str'>
<class 'tuple'>
localhost:8888/nbconvert/html/Chapter10_tuples.ipynb?download=false 1/7
4/4/2020 Chapter10_tuples
In [8]:
t=tuple()
print(t)
()
In [9]:
#if the argument is a sequence (string, list or tuple), the result of call to tuple is
a tuple with elements of the sequence:
#tuple is the name of constructor
t=tuple('apple')
print(t)
In [13]:
tup2=(1,2,3,4,5)
print(tup2[0])
print(tup2[1])
print(tup2[10]) #10 is not in range
1
2
--------------------------------------------------------------------------
-
IndexError Traceback (most recent call las
t)
<ipython-input-13-2318b0c9a894> in <module>
4 print(tup2[0])
5 print(tup2[1])
----> 6 print(tup2[10])
In [14]:
(2, 3)
localhost:8888/nbconvert/html/Chapter10_tuples.ipynb?download=false 2/7
4/4/2020 Chapter10_tuples
In [15]:
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
<ipython-input-15-2a2d2654785a> in <module>
1 #trying to modify elements in tuple
2 t=(1,2,3,4,5)
----> 3 t[0]='A'
In [17]:
('A', 3, 4, 5)
Comparing tuples
• Python starts by comparing the first element from each sequence. If they are equal, it goes on to the next
element, and so on, until it finds elements that differ. Subsequent elements are not considered (even if they
are really big).
Example
In [18]:
(0,1,2)<(0,3,4)
Out[18]:
True
In [19]:
(0,1,2)>(0,3,4)
Out[19]:
False
DSU Pattern
localhost:8888/nbconvert/html/Chapter10_tuples.ipynb?download=false 3/7
4/4/2020 Chapter10_tuples
It sorts primarily by first element, but in the case of a tie, it sorts by second element, and so on.
Decorate a sequence by building a list of tuples with one or more sort keys preceding the elements from the
sequence, Sort the list of tuples using the Python built-in sort, and Undecorate by extracting the sorted
elements of the sequence.
In [23]:
#For example, suppose you have a list of words and you want to sort them from longest t
o shortest:
res = list()
for length, word in t:
res.append(word)
print(res)
Tuple Assignment
One of the unique syntactic features of the Python language is the ability to have a tuple on the left side of an
assignment statement. This allows you to assign more than one variable at a time when the left side is a
sequence.
In [24]:
have
fun
localhost:8888/nbconvert/html/Chapter10_tuples.ipynb?download=false 4/7
4/4/2020 Chapter10_tuples
In [25]:
have
fun
In [26]:
m = [ 'have', 'fun' ]
(x, y) = m
print(x)
print(y)
have
fun
In [30]:
# tuple assignment allows us to swap the values of two variables in a single statement:
a,b=1,2
a,b = b,a
print(a)
print(b)
2
1
In [33]:
mymail
gmai.com
Example
localhost:8888/nbconvert/html/Chapter10_tuples.ipynb?download=false 5/7
4/4/2020 Chapter10_tuples
In [38]:
In [40]:
a 10
d 1
c 22
In [41]:
localhost:8888/nbconvert/html/Chapter10_tuples.ipynb?download=false 6/7
4/4/2020 Chapter10_tuples
In [42]:
import string
fhand = open('test.txt')
counts = dict()
for line in fhand:
line = line.translate(str.maketrans('', '', string.punctuation))
line = line.lower()
words = line.split()
for word in words:
if word not in counts:
counts[word] = 1
else:
counts[word] += 1
lst.sort(reverse=True)
3 the
2 than
2 more
2 has
1 with
1 united
1 states
1 spot
1 pandemic
1 new
Example :
Assuming that we have defined the variables last, first, and number, we could write a dictionary assignment
statement as follows:
directory[last,first] = number
The expression in brackets is a tuple. We could use tuple assignment in a for loop to traverse this dictionary.
This loop traverses the keys in directory, which are tuples. It assigns the elements of each tuple to last and
first, then prints the name and corresponding telephone number.
localhost:8888/nbconvert/html/Chapter10_tuples.ipynb?download=false 7/7