Page 305 - Computer Science Class 11 Without Functions
P. 305
4. sum(tpl [,num]) It returns the sum of all values >>> tpl =(1, 3, 5, 7)
in the tuple: tpl. The optional >>> sum(tpl)
argument num, when provided, 16
is added to the sum of the >>> sum(tpl, 5)
elements of the tuple. 21
5. It returns the mean of the >>> from statistics import mean
mean(tpl) elements of a tuple. >>> tpl = (4, 2, 5, 9)
>>> mean(tpl)
5
12.11 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)
12.11.1 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')
12.11.2 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
()
12.11.3 Membership Operator in
>>> 'Good Afternoon' in greetings # Membership operation in
True
Program 12.4 Write a program that takes a list of words as an input and displays a list of tuples where each tuple
comprises a word-length pair, that is, word and its corresponding length.
01 '''
02 Objective: To return word-length pairs for the words in a list.
03 Input: lst - list containing words
04 Output: List of (word, length) tuples
05 '''
06 '''
07 Approach:
08 initialise an empty word-length list wordLenPairs
09 for each word in lst:
10 append the tuple (word, length) to wordLenPairs
11 '''
12
13 lst = eval(input('Enter the list: '))
Lists and Tuples 303

