# Class 11 Computer Science Ch 10 Tuples and Dictionary in Python NCERT Book Exercise Solution

### Chapter – 10 : Tuples and Dictionary

Summary (A Quick Recap)

• Tuples are immutable sequences, i.e., we cannot change the elements of a tuple once it is created.

• Elements of a tuple are put in round brackets separated by commas.

• If a sequence has comma separated elements without parentheses, it is also treated as a tuple.

• Tuples are ordered sequences as each element has a fixed position.

• Indexing is used to access the elements of the tuple; two way indexing holds in dictionaries as in strings and lists.

• Operator ‘+’ adds one sequence (string, list, tuple) to the end of other.

• Operator ‘*’ repeats a sequence (string, list, tuple) by specified number of times

• Membership operator ‘in’ tells if an element is present in the sequence or not and ‘not in’ does the opposite.

• Tuple manipulation functions are: len(), tuple(), count(), index(), sorted(), min(), max(),sum().

• Dictionary is a mapping (non-scalar) data type. It is an unordered collection of key-value pair; keyvalue pair are put inside curly braces.

• Each key is separated from its value by a colon.

• Keys are unique and act as the index.

• Keys are of immutable type but values can be mutable.

Class 11 Computer Science – NCERT Book Exercise Solution

#### Chapter 10. Tuples and Dictionary

1. Consider the following tuples, tuple1 and tuple2:

tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)

Find the output of the following statements:

i. print(tuple1.index(45))

ii. print(tuple1.count(45))

iii. print(tuple1 + tuple2)

Answer: (23, 1, 45, 67, 45, 9, 55, 45, 100, 200)

iv. print(len(tuple2))

v. print(max(tuple1))

vi print(min(tuple1))

vii. print(sum(tuple2))

viii. print(sorted(tuple1))

print(tuple1)

Answer: [1, 9, 23, 45, 45, 45, 55, 67]

(23, 1, 45, 67, 45, 9, 55, 45)

2. Consider the following dictionary stateCapital:

Find the output of the following statements:

i. print(stateCapital.get(“Bihar”))

ii. print(stateCapital.keys())

iii. print(stateCapital.values())

iv. print(stateCapital.items())

Answer: dict_items([(‘Assam’, ‘Guwahati’), (‘Bihar’, ‘Patna’), (‘Maharashtra’, ‘Mumbai’), (‘Rajasthan’, ‘Jaipur’)])

v. print(len(stateCapital))

vi. print(“Maharashtra” in stateCapital)

vii. print(stateCapital.get(“Assam”))

viii. del stateCapital[“Rajasthan”]

print(stateCapital)

Answer: {‘Bihar’: ‘Patna’, ‘Maharashtra’: ‘Mumbai’, ‘Rajasthan’: ‘Jaipur’}

3. “Lists and Tuples are ordered”. Explain.

Answer: Lists and Tuples are ordered, it means that both the lists’ and tuples’ elements are ordered, because each element in lists and tuples has a designated index position. The index position (either forward or backward), maintain the order and these elements can also accessed individually with the help of their index position.

For example :

List1 = [10, 3, ‘mycstutorial.in’, False]

Tuple1 = [25, 12, ‘mycstutorial.in’, True]

print(List1)

print(Tuple)

Output:

myctutorial.in

True

4. With the help of an example show how can you return more than one value from a function.

Answer: A function can return multiple values in the form of tuples.

For example :

```#Example - 1
def square(n1, n2, n3):
sq = n1**2, n2**2, n3**2
rerurn sq

#Example - 2
def cube(n1, n2, n3):
return n1**3, n2**3, n3**3

#__main__

print("Calling Method Square")
result1 = square(5,10,15)
print(result1)
print(type(result1))

print("Calling Method Cube")
result2 = cube(2,3,4)
print(result2)
print(type(result2))
```

Output:

Calling Method Square

(25, 100, 225 )

<class ‘tuple’>

Calling Method Cube

(8, 27, 64)

<class ‘tuple’>

5. What advantages do tuples have over lists?

Answer: The advantages of tuples over lists is that tuples ensure that the data stored in tuple will not change.

If you want to store data as a sequence of any type of values and does not want to make any changes accidently, then you can use Tuple instead of List.

6. When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.

Answer: Tuples are used to store the data which is not intended to change during the course of execution of the program. For example, if the name of months is needed in a program, then the same can be stored in the tuple as generally, the names will either be repeated for a loop or referenced sometimes during the execution of the program.

Dictionary is used to represent the unordered list of the data. The key and value are separated by colon. Dictionary is used to store associative data like student’s roll no. and the student’s name. Here, the roll no. will act as a key to find the corresponding student’s name. The position of the data doesn’t matter as the data can easily be searched by using the corresponding key.

For example :

```#Tuple vs Dictionary

month = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

stud = {1: ('Amrit', 'IV','DPS Mgarh'), 2: ('Tanmay', 'III', 'DPS Mgarh')}

print("Name of month 6th : ", month)

print("Details of Roll Number 1: ")
print(stud)
```

Output:

Name of month 6th : Jun

Details of Roll Number 1:

(‘Amrit’, ‘IV’, ‘DPS Mgarh’)

7. Prove with the help of an example that the variable is rebuilt in case of immutable data types.

Answer: A tuple once created cannot be changed Even if you try to make changes in the tuple and give it the same name, Python Internally will create a new tuple and store it at a different memory location. For example

```>>> tup1 = (1,2,'mycstutorial.in')
>>> id(tup1)
61944840
>>> print(tup1)
(1, 2, 'mycstutorial.in')

>>> tup1 = tup1 + ('ncert solution','xi','computer','science')
>>> tup1
(1, 2, 'mycstutorial.in', 'ncert solution', 'xi', 'computer', 'science')
>>> id(tup1)
59301336
>>> print(tup1)
(1, 2, 'mycstutorial.in', 'ncert solution', 'xi', 'computer', 'science')
>>> ```

Check the id printed in two different statement. As you can observe in both cases variable name is the same but it is printed different id’s, It means that tuple is immutable data type, and if you try to change it, it will not make the change in-place, it create a new variable.

8. TypeError occurs while statement 2 is running. Give reason. How can it be corrected?

tuple1 = (5) #statement 1
len(tuple1) #statement 2

Answer: tuple1 = (5) statement does not create a tuple type variable, it creates integer type variable. len() function works with collection/sequence type variable such tuple, list, dictionary, string.

len() function need sequence value or variable as argument. That’s why len(tuple1) generate an error TypeError.

Corrected Way :

tuple1 = (5,) #statement 1
len(tuple1) #statement 2

Output:

1