Page 352 - Computer Science Class 11 With Functions
P. 352
13.8 Tuple Operations
Likewise, strings and lists, Python also supports a variety of operations on tuples. Below we illustrate some important
operations on tuples.
>>> greetings = ('Good Morning', 'Good Afternoon')
>>> marks = (78, 99, 34, 45)
>>> dateOfBirth = (1, 'October', 1990)
● Concatenation Operator +: The concatenation operator concatenates and yields a tuple comprising the elements
of the first tuple, followed by the elements of the second tuple.
>>> dayGreetings = greetings + ('Good Night',)
>>> dayGreetings
('Good Morning', 'Good Afternoon', 'Good Night')
● Multiplication Operator *: The multiplication operator concatenates the string the specified number of times.
>>> greetings * 2
('Good Morning', 'Good Afternoon', 'Good Morning', 'Good Afternoon')
>>> greetings * 0
()
● Membership operator in:
>>> 'Good Afternoon' in greetings # Membership operation in
True
Program 13.4 Define a function wordLength(lst) which takes a list of words and returns a list of tuples
where each tuple comprises a word and its corresponding length. Make use of the function wordLength() to
write a program that accepts a list of words from a user and prints a list of tuples where each tuple is word-length
pair, that is, word and its corresponding length.
01 def wordLength(lst):
02 '''
03 Purpose: To return word-length pairs for the words in a list.
04 Input Parameter: lst - list containing words
05 Return Value: List of (word, length) tuples
06 '''
07 '''
08 Approach:
09 initialise an empty word-length list wordLenPairs
10 for each word in lst:
11 append the tuple (word, length) to wordLenPairs
12 '''
13 wordLenPairs = list()
14 for word in lst:
15 wordLenPairs.append((word, len(word)))
16 return wordLenPairs
17
18
19 #Objective: Accept a word-list from a user and print list of (word.lenngth)tuples
20
21 lst = eval(input('Enter the list: '))
22 print('List of tuples:: ', wordLength(lst))
Sample Output:
>>> Enter the list: ['ORANGE','EDUCATION']
List of tuples:: [('ORANGE', 6), ('EDUCATION', 9)]
350 Touchpad Computer Science-XI

